Why can't arrays of same type and size be assigned?

arrays, c

Solution

The problem with arrays is that in all expressions (except when passed to the `sizeof` and the unary `&` operators) they convert to a pointer to their first element.

So, supposing you have:

int arr1[10];
int arr2[10];
...

Then if you write something like

arr1 = arr2;

you are actually attempting to do this:

arr1 = &arr2[0];

or this:

&arr1[0] = &arr2[0];

In both cases you have a problem preventing your code from compiling. In the former case you're attempting to assign between two incompatible types (array vs pointer), while in the latter case you're attempting to modify a constant pointer (`&arr1[0]`).

Problem

If I declare two arrays - `arr1` and `arr2` - of, say, type `int` of size 10 each, and initialize first array, and I wish to create a copy of `arr1` in `arr2`; why can't I just give the instruction `arr2 = arr1` ? I know two structures of same type can be assigned. Why is that not the case with arrays?

Original source

Related problems