我有以下代码。我试图将一个结构复制到一个字符串。我想了解为什么输出在strncpy和memcpy之间变化。用于结构复制的memcpy和strncpy的区别
#include <stdio.h>
#include<string.h>
struct a{
int len;
int type;
};
int main(){
struct a aa={98,88};
char str[10]="";
char str2[10]="";
strncpy(str,&aa,sizeof(struct a));
memcpy(str2,&aa,sizeof(struct a));
for(int i=0;i<10;i++)printf("%2d",str[i]);
printf("\n");
for(int i=0;i<10;i++)printf("%2d",str2[i]);
return 0;
}
下面是输出:
98 0 0 0 0 0 0 0 0 0
98 0 0 088 0 0 0 0 0
我明白strncpy()函数将复制直到遇到 '\ 0'(或大小限),但我没有 '\ 0' 值在结构中。有人可以帮助我理解这一点。 这样做的目的:试图通过网络发送结构。虽然我打算实现系列化,我想了解的行为
编辑: 1)由基思·汤普森
建议下面是生成警告。
incompatible pointer types passing 'struct a *' to parameter of type 'const char *' [-Wincompatible-pointer-types]
2)I修改代码中的位,以使用int数组:
(把此供参考我明白,在这种情况下,memcpy的拷贝结构体中的前两个元素的变量。阵列的大小是足够的结构变量)
#include <stdio.h>
#include<string.h>
struct a{
int len;
int type;
};
int main(){
struct a aa={98,88};
int str[10]={0};
int str2[10]={0};
strncpy(str,&aa,sizeof(struct a));
memcpy(str2,&aa,sizeof(struct a));
for(int i=0;i<10;i++)printf("%2d",str[i]);
printf("\n");
for(int i=0;i<10;i++)printf("%2d",str2[i]);
return 0;
}
下面是邻\号码:
98 0 0 0 0 0 0 0 0 0
9888 0 0 0 0 0 0 0 0
下面生成的警告:
incompatible pointer types passing 'int [10]' to parameter of type 'char *' [-Wincompatible-pointer-types]
incompatible pointer types passing 'struct a *' to parameter of type 'const char *' [-Wincompatible-pointer-types]
您的结构不是字符串。 'strncpy'对字符串进行操作。这个电话甚至不应该编译;你至少应该得到一个'struct a *'参数传递给'strncpy'的警告,它需要'char *'。即使对于字符串,通常也应该避免使用“strncpy”。 [见我在这里主题的咆哮](http://the-flat-trantor-society.blogspot.com/2012/03/no-strncpy-is-not-safer-strcpy.html)。 – 2014-12-01 20:08:26
它的确发出了警告。 – mayur 2014-12-02 10:49:19
请更新您的问题以显示确切的警告;这是非常重要的信息。 – 2014-12-02 14:54:12