C中结构体是否能用=运算符直接赋值的问题

印象中,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
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值