我是靠谱客的博主 俭朴冥王星,最近开发中收集的这篇文章主要介绍[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%xn",&array1);
printf("address(array1+1):0x%xn",&array1+1);
printf("(array1==&array[0]):0x%xn",array1);
printf("(&array1[0]+1):0x%xn",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

最后

以上就是俭朴冥王星为你收集整理的[Error] assignment to expression with array type的全部内容,希望文章能够帮你解决[Error] assignment to expression with array type所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部