印象中,C中的结构体是不能用等号运算符赋值的,以前写的程序对两个结构体内容交换的时候,都是单独建立一个swap函数来对两个结构体的各个成员逐一交换。
然而,查阅资料加实践后,发现=是可以用来给结构体赋值的。
首先来看一下文献:
C99标准§6.7.2.1上说:
struct s { int n; double d[]; };
……
struct s *s1;
struct s *s2;
……
The assignment:
*s1 = *s2;
only copies the member n and not any of the array elements.
而C99标准的TC2 Committee Draft则进一步指出:
if any of the array elements are within the first sizeof (struct s) bytes
of the structure, they might be copied or simply overwritten with indeterminate values.
【http://bbs.csdn.net/topics/110160458】
实践的效果是
运行平台:win10 64位
visual Studio 2013
#include<stdio.h>
#include&l