【发布时间】:2014-12-01 19:10:12
【问题描述】:
我有以下代码。我正在尝试将结构复制到字符串。我想了解为什么输出在 strncpy 和 memcpy 之间变化。
#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)我稍微修改了代码以使用 int 数组:
(作为参考。我知道在这种情况下,memcpy 将 struct 的变量复制到数组的前两个元素中,因为大小足以容纳 struct 变量。)
#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;
}
下面是o\p:
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通常也应该避免; see my rant on the topic here. -
它确实引发了警告。
-
请更新您的问题以显示确切的警告;这是非常重要的信息。
-
感谢@KeithThompson 添加了警告。还添加了另一个示例