2

I am new to C programming, and there is a question that bothers me. Let's see the following piece of code:

int main()
{
   int arr[3]={1,2,3};
   printf("%d\n", arr);
   printf("%d", &arr);
}

If I run this code, it gives the same result for both arr and &arr. But why arr=&arr? I have seen quite a lot of answers on this topic, but none of them is clear to me. In a nutshell, people agree that an array variable holds the address of the first element of the array, e.g., arr holds the address of arr[0], say arr= 4340. Then the variable arr must be stored somewhere in the memory and &arr is the address of the memory cell that stores the value 4340. If &arr = arr then it turns out that 4340 is stored at address 4340. But the value at address 4340 is the value of the first element of the array, so it should be 1??? I am very confused at this point and would really appreciate some help.

Nguyen
  • 25
  • 3
  • 1
    Read section 6 of the [comp.lang.c FAQ](http://www.c-faq.com/). It's a good explanation of the often confusing relationship between arrays and pointers in C. (Rule 1: arrays are not pointers.) – Keith Thompson Oct 06 '16 at 15:35
  • @Keith Thompson: Thanks for the reference! I will definitely look at it. – Nguyen Oct 10 '16 at 07:41

2 Answers2

5

Your confusion here has to do with the equivalency of arrays an pointers. Namely, arrays are not pointers but in some contexts an array decays into a pointer.

An array does not hold the address to the first element, but is a contiguous set of elements of the base type. The address of the array itself happens to be the address of the first element.

One place where the difference between a pointer and an array is significant is with the sizeof operator which can tell the difference. Assuming an int is 4 bytes and a pointer is 8 bytes, sizeof(arr) will give you 12, while sizeof(&arr) will give you 8.

When you pass arr to a function, at that point it decays into a pointer to the first element. So printing arr and &arr will give you the same value.

That being said, you should always use %p to print a pointer instead of %d, as this can cause undefined behavior.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Really it causes undefined behavior? I thought it would just print an incorrect value on platforms where `sizeof(int) != sizeof(void*)` – Alex Díaz Oct 06 '16 at 15:57
  • 2
    @AlejandroDíaz An address may not necessarily be a number. On DOS for example, an address is not a single number but two numbers. – dbush Oct 06 '16 at 16:01
2

The behaviour of your program is undefined. You cannot use %d as the format specifier for an array type or the address of an array type. You could have passed arr[0] to get the value of the first element in that array.

Occasionally arr does decay to a pointer of type int*, such as when it is passed to a function. The fact that arr is equal to &arr in the context of their being a parameter to printf with the format specifier corrected to %p is also due to pointer decay.