1

Say I have a structure of:

typedef struct{
    int data[5];
}ArrayList;

With a main function of:

int main(int argc, char *argv[]) {
    ArrayList test = {1,2,3,4,5};
    int x;
    change(test);
    return 0;
}

change function body:

void change(ArrayList arr){
     printf("%d",arr.data);
}

The way I understand this is that since it's a pass by copy, it passes down the value of test, and arr takes that value.

Since int data[5] is an array, it can't actually pass down all its actual members at once, in this case, the integers, so it gives the address of the first member (test.data[0]). It can only do so one by one like data[0], data[1], ... So I'm assuming that arr.data here should have the value as test.data (talking about the pointer to the first member, so the address).

But for some reason if I print the value of arr.data it's displaying a completely different address from test.data's value, and when I print the members of arr it has all the members of test.data.

I'm seeing it as something similar if I declared something like:

int data[5] = {1,2,3,4,5};
printf("%d", data);
change(data);

then

void change(int data[]){
   printf("%d", data); // this would have the same value as the statement above.
}

How does arr.test get its value?

codeorig12
  • 43
  • 4
  • Save time, enable all compiler warnings. `printf("%d",arr.data);` is bad code. `"%d"` matches an `int`. `arr.data` is not an `int`. – chux - Reinstate Monica Aug 27 '21 at 09:59
  • 1
    thanks for the comment. I tried %p specifier to check the results, but it still is displaying the same result. still different addresses. – codeorig12 Aug 27 '21 at 10:03
  • Related question: [What does impossibility to return arrays actually mean in C?](https://stackoverflow.com/questions/50808782/what-does-impossibility-to-return-arrays-actually-mean-in-c) – Steve Summit Aug 27 '21 at 11:21

1 Answers1

2

Function arguments are passed by value. A function parameter is a variable local to the function that is initialized with the value of the corresponding argument.

The argument test and the parameter arr are different objects. They are in different places in memory, and the arrays in them are in different places in memory, so they have different addresses.

Since int data[5] is an array, it can't actually pass down all its actual members at once,…

This is not correct. C does not provide any feature for you to copy an array by its “value” (which is an aggregate “value” of all its members), but you can copy a structure containing an array, and that will copy the contained array.

Incidentally, to print addresses properly, convert them to void * and format them with %p: printf("%p\n", (void *) arr.data);.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • but aren't test.data and arr.data similar to pointers, in that they contain the address of the first member? as pointers why is the address contained in arr.data different from the one contained by test.data? – codeorig12 Aug 27 '21 at 10:11
  • 1
    @codeorig12: No, an array does not contain the address of its first element. When an array is used in an expression, it is automatically **converted** to a pointer to its first element except when it is used as the operand of unary `&`, is used as the operand of `sizeof`, or is a string literal used to initialize an array. That conversion merely takes the address of where the first element is in memory. It does not fetch it from anything the array contains. An array is a contiguous sequence of elements in memory. It is not a pointer. – Eric Postpischil Aug 27 '21 at 10:12
  • that makes a lot of sense now that you say that, I'v always assumed that an array is just a pointer pointing to a contiguous block of memory. – codeorig12 Aug 27 '21 at 10:17