[Error] assignment to expression with array type

int array1[] = {1,2,3};

int *array2;

int array3[3];

array2 = array1;//copy pointer only

array3 = &array1;//error

To elaborate, from C11, chapter §6.5.16

assignment operator shall have a modifiable lvalue as its left operand.

and, regarding the modifiable lvalue, from chapter §6.3.2.1

A modifiable lvalue is an lvalue that does not have array type, [...]

You need to use strcpy() to copy into the array. 

That said, int array1[] = {1,2,3}; works fine, because this is not a direct assignment involving assignment operator. There we're using a brace-enclosed initializer list to provide the initial values of the object. That follows the law of initialization, as mentioned in chapter §6.7.9

Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union.[....]

Byte the way:

printf("address(array1):0x%x\n",&array1);
printf("address(array1+1):0x%x\n",&array1+1);
printf("(array1==&array[0]):0x%x\n",array1);
printf("(&array1[0]+1):0x%x\n",array1+1);
/*
address(array1):0x62fde0
address(array1+1):0x62fdec
(array1==&array[0]):0x62fde0
(&array1[0]+1):0x62fde4
*/

array1 and &array1 have same address: 0x62fde0.BUT array1 represents &array1[0]. &array1 represents the whole array.

Referenc: https://stackoverflow.com/questions/37225244/error-assignment-to-expression-with-array-type-error-when-i-assign-a-struct-f/37225315#37225315?newreg=2e0f0ad5ee8a4906997213832707b1e6

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值