3

As described in the answer to this question Copying one structure to another, We can copy the contents of a structure element to another by simple assignment. e1=e2;

But this simple assignment does not work when copying array elements. Can someone offer an explanation?

Thanks.

Community
  • 1
  • 1
sheetal_158
  • 7,391
  • 6
  • 27
  • 44
  • "*does not work when copying array elements*" please give an example where copying an array element does not work. – Weather Vane Dec 26 '14 at 17:57
  • Please show a piece of code that does not work: assigning array elements that happen to be `struct`s will work fine, it's an array-to-array assignment that's not going to work. – Sergey Kalinichenko Dec 26 '14 at 17:58
  • 1
    It's mostly a historical artifact. You can assign a `struct wrapped_array { int x[10]; };` just fine. – Rufflewind Dec 26 '14 at 18:25

3 Answers3

1

Arrays are second-class citizens in C: you cannot assign an array to an array and you cannot return an array from a function.

Chris Torek offers this explanation in comp.lang.c:

"Note that V6 C also did not support struct-valued arguments and struct-valued return values. One might, then, imagine that Dennis figured that any return value that did not fit in a register was too much work to put into the compiler at that point."

ouah
  • 142,963
  • 15
  • 272
  • 331
  • It is worth noting that at least on the theoretical level, the standard could have easily supported copying one array into another, as well as returning a local array from a function. Compilers would have just as easily been able to support that (just as they support copying and returning structures). Why the standard has not updated to support this up until these very days - is beyond my comprehension. – barak manos Dec 26 '14 at 18:29
1

Array is not a modifiable lvalue ("something that has a location (in memory)"). This means that although it is a lvalue, it can't be a left operand of assignment operator =.

In case of structure, other than assignment*, C provides no operations on entire structures. One can't use ==, != operators to test whether two structures are equal or not.

You can create dummy structures to enclose arrays that will be copied later:

struct
{
    int arr[5];
} arr1, arr2;

Latter you can assign

arr1 = arr2;  

*The = operator can be used only with compatible structure types.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

The name of an array is an alias of the address of the first element of the array*:

#include <stdio.h>

int main()
{
    int foo[5] = {2, 3, 4, 5, 6};
    int *bar = foo; 
    // now bar == foo, i.e. bar == &foo[0], so changing bar[2] changes foo[2]
    // bar[2] works because of pointer arithmetic

    printf("    foo is: %p\n",foo);     // prints the address of the first element of foo
    printf("&foo[0] is: %p\n", &foo[0]); // also prints the address of the first element of foo
    printf("    bar is: %p\n", bar);
    printf("&bar[0] is: %p\n", &bar[0]);
    printf(" foo[2] is: %d\n", foo[3]);
    printf(" bar[2] is: %d\n", bar[3]);

    return 0;

}

*with some exceptions. Namely sizeof foo != &foo[0]. See How come an array's address is equal to its value in C?

So when you wrote arr1 = arr2 the compiler thinks you are just referring to the address of the array not the whole array. When you write the name of a struct, the compiler knows you are referring to the struct as a whole, not just the first member.

Community
  • 1
  • 1
schrödinbug
  • 692
  • 9
  • 19