c语言结构体按地址赋值,C语言中结构体的直接赋值

比如: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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值