t2.mark=t1.mark;//mark为
结构体中的数组,不能直接赋值??
是的,数组名是个静态地址值,不能被修改,如果想把数据存储到其中,可以用memcpy()函数进行内存拷贝,或采用循环进行赋值
#include <string.h>
memcpy( t2.mark, t1.mark, sizeof(t2.mark) );
追问能否敲个程序具体举例一下啊,不太明白该如何赋值
追答#include <stdio.h>
#include <string.h>
struct{char mark[12];int num1;double num2;}t1,t2;
void main()
{
t1.num1=1;
t1.num2=1.1;
strcpy( t1.mark,"hello" );
memcpy( t2.mark, t1.mark, sizeof(t2.mark) ); //只对t2.mark数组赋值
t2.num1=2;
t2.num2=2.3;
printf("t1=[%s %d %f]\n", t1.mark,t1.num1,t1.num2 );
printf("t2=[%s %d %f]\n", t2.mark,t2.num1,t2.num2 );
t2=t1 ; //结构体完整拷贝
printf("t1=[%s %d %f]\n", t1.mark,t1.num1,t1.num2 );
printf("t2=[%s %d %f]\n", t2.mark,t2.num1,t2.num2 );
}
本回答被网友采纳