I'm programming in C and wonder if it's possible to assign multiple values at once to a multi-dimensional array? I have tried some technique but all have failed! I’m NOT interested to loop through the array to assign values (I want the fasted way to assign new values to all index in the array). The array I’m working with: ary[4][4].
Since an array is not a modifiable lvalue, it cannot appear on the left side of an assignment. You can initialize it and you can assign individual members via indexing.
6.3.2.1
A modifiable lvalue is an lvalue that does not have array type, does not have ...
And a modifiable lvalue
:
The name ‘‘lvalue’’ comes originally from the assignment expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue.
So no, you can't do what you want.
memcpy from another one will overwrite what is already in ary
.
int ary[4][4];
int another[4][4] = {{1,2,3,4}, {5,6,7,8}, {1,2,3,4}, {5,6,7,8}};
memcpy(ary, another, 4 * 4 * sizeof(int));
来源:https://stackoverflow.com/questions/11733919/how-to-assign-multiple-values-at-once-to-multi-dimensional-array-after-creating