比如:struct A{char v1[20];int v2;} a,b;a = b;这是没有任何问题的赋值.struct B{char *v1;int v2;} c,d;c = d;这种结构体赋值,就需要注意(包括在C++里)。对于指针赋值,它不是数据进行了复制保存而是多了一个指针指向而已,这样一旦b对象释放,a的指向就成了非法的垃圾数据。所以在 C/C++ 中,有些需要要另外自己定义复制函数的。
C语言直接支持结构体变量的赋值,它是按值逐元素赋值的。参看K&R A.7.17 Assignment ExpressionsIn the simple assignment with =, the value of the expression replaces that of the object referred to by the lvalue. One of the following must be true: both operands have arithmetic type, in which case the right operand is converted to the type of the left by the assignment; or both operands are structures or unions of the same~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~type; or one operand is a pointer and the other is a pointer to void, or the left~~~~operand is a pointer and the right operand is a constant expression with value 0; or both operands are pointers to functions or objects whose typ