C中结构体是否能用=运算符直接赋值的问题
2017-05-18 22:06
323 查看
印象中,C中的结构体是不能用等号运算符赋值的,以前写的程序对两个结构体内容交换的时候,都是单独建立一个swap函数来对两个结构体的各个成员逐一交换。
然而,查阅资料加实践后,发现=是可以用来给结构体赋值的。
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】
visual Studio 2013
运行结果:
60
1,M,SecondDept,SecondName,1,1,Ganlv
0,F,FirstDept,FirstName,0,0,Cantjie
0,F,FirstDept,FirstName,0,0,Cantjie
1,M,SecondDept,SecondName,1,1,Ganlv
在这个程序中,memcpy与=的效果相同。
值得注意的是,这种赋值方式是“浅赋值”,当结构体中含有指针时,并不会使temp中的char *p另外开辟一个空间用于存储字符串。
相关文章:http://blog.csdn.net/hazir/article/details/9429017
然而,查阅资料加实践后,发现=是可以用来给结构体赋值的。
首先来看一下文献:
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<string.h> char str1[10] = "Cantjie"; char str2[10] = "Ganlv"; struct emp{ int wage; char sex; char dept[20]; char name[20]; int num[2]; char *p; }a[10] = { 0, 'F', "FirstDept", "FirstName",0,0,str1, 1,'M',"SecondDept","SecondName",1,1 ,str2}; int main() { int i, size; struct emp temp; struct emp *p = &temp; size = sizeof(struct emp); printf("%d\n", size); memcpy(p, &a[0], size); memcpy(&a[0], &a[1], size); memcpy(&a[1], p, size); printf("%d,%c,%s,%s,%d,%d,%s\n", a[0].wage, a[0].sex, a[0].dept, a[0].name, a[0].num[0], a[0].num[1], a[0].p); printf("%d,%c,%s,%s,%d,%d,%s\n", a[1].wage, a[1].sex, a[1].dept, a[1].name, a[1].num[0], a[1].num[1], a[1].p); temp = a[0]; a[0] = a[1]; a[1] = temp; printf("%d,%c,%s,%s,%d,%d,%s\n", a[0].wage, a[0].sex, a[0].dept, a[0].name, a[0].num[0], a[0].num[1], a[0].p); printf("%d,%c,%s,%s,%d,%d,%s\n", a[1].wage, a[1].sex, a[1].dept, a[1].name, a[1].num[0], a[1].num[1], a[1].p); }
运行结果:
60
1,M,SecondDept,SecondName,1,1,Ganlv
0,F,FirstDept,FirstName,0,0,Cantjie
0,F,FirstDept,FirstName,0,0,Cantjie
1,M,SecondDept,SecondName,1,1,Ganlv
在这个程序中,memcpy与=的效果相同。
值得注意的是,这种赋值方式是“浅赋值”,当结构体中含有指针时,并不会使temp中的char *p另外开辟一个空间用于存储字符串。
相关文章:http://blog.csdn.net/hazir/article/details/9429017
相关文章推荐
- 两个结构体可以直接赋值吗? - 回复 "JohnsonAnother" 的问题
- C# 结构体直接赋值的问题
- 有关c语言结构体能否直接赋值的问题
- 为结构体中的CString成员赋值出错的问题
- Linux shell脚本中执行命令结果赋值给变量&&echo输出变量是否包含换行符的问题
- Java中long类型直接赋值出现The literal xxxxxxxxxx of type int is out of range 问题的原因和解决方法
- 关于”栈“:判断计算式的运算符(),[]应用是否真确的问题
- 结构体赋值问题
- hdu 6113 度度熊的01世界(结构体的赋值问题)
- Java中long类型直接赋值大数字的问题
- 结构体变量中数组成员赋值问题
- 一个关于结构体赋值的问题
- C语言结构体对齐存储问题以及给结构体中的数组的赋值问题
- php foreach 使用&(与运算符)引用赋值要注意的问题
- 结构体中的数组成员的赋值问题
- 结构体可直接赋值
- c语言结构体可以直接赋值
- Java中long类型直接赋值大数字需要注意的问题
- C语言结构体对象间直接赋值
- javascript 直接创建对象与间接创建对象 私有函数不能访问this变量 是否使用var声明变量几个问题探讨