7
#include <stdio.h>

int main(){
    int array[] = {1,2,3};
    printf("L00: array[0]=%d \n",array[0]);
    printf("L01: array[1]=%d \n",array[1]);
    printf("L02: array[2]=%d \n",array[2]);
    printf("L03: &array[0]=%p \n",&array[0]);
    printf("L04: &array[1]=%p \n",&array[1]);
    printf("L05: &array[2]=%p \n",&array[2]);
    printf("L06: array=%p \n",array);
    printf("L07: &array=%p \n",&array);
    return 0;
}

In this program, L03,L06, and L07 show (&array,array, and &array[0], respectively) the same memory address.

So I'm curious about the location of the "array" as a variable in RAM.

If there is no location of "array" in RAM, where is it?

3
  • This might be interest you. Commented Jun 16, 2017 at 6:58
  • Why exactly do you ask, and why should you really care? Commented Jun 16, 2017 at 7:17
  • 1
    Please clarify the question, are you asking about why you have three pointers to the same location? Or are you asking why the array is placed where it is? Commented Jun 16, 2017 at 7:22

4 Answers 4

9

I must have written this many times, but lets do it again:

+----------+----------+----------+
| array[0] | array[1] | array[2] |
+----------+----------+----------+
^          ^          ^          ^
|          |          |          |
array      |          |          |
|          |          |          |
array + 0  array + 1  array + 2  |
|          |          |          |
&array[0]  &array[1]  &array[2]  |
|                                |
&array                           |
|                                |
&array + 0                       &array + 1

As you can see from the above "image" the first element can be reached through five pointers (two really, array decays to &array[0], and array + 0 is equal to array).

The type of these pointers may be different though:

  • &array is of type int (*)[3]
  • &array + 0 is equal to &array and is therefore of the same type
  • &array[0] is of type int *
  • array decays to &array[0] and is therefore of the same type
  • array + 0 is equal to array and is therefore of the same type

There are of course other ways of getting pointers to a specific element, but they are really just variants of the above. For example (&array)[0] is equal to &array + 0 which is equal to &array.

Another thing that is good to know is that for any pointer or array p and index i, the expression p[i] is exactly the same as *(p + i).


The biggest thing you should learn from this is the difference between array and &array. Even though they both point to the same location, their types are different which makes it very different semantically.

Sign up to request clarification or add additional context in comments.

1 Comment

@Some programmer dude Thanks. So "array" itself has no memory location, right??
8

The C11 standard specification does not say anything about RAM, IIRC (but check that by yourself, read n1570). On most operating systems, a process has a virtual address space (and your program would run in that space, not necessarily in RAM).

Your array is (likely to be) located on the call stack.

I guess that an hypothetical optimizing compiler might be clever enough to put it elsewhere (but I cannot name such a compiler).

1 Comment

llvm (clang) with the "safe stack" option would put it on a separate stack (to protect against return address corruption and such). Just an example for your last statement.
6

The C standard does not mandate where the array is stored, other than it having "automatic storage duration" and local scope. Larger variables with automatic storage duration are almost certainly stored on the stack.

As for why those 3 different forms of syntax give the same address:

  • array, whenever used in an expression, decays into a pointer to the first element. Making it 100% equivalent to &array[0].
  • &array is a pointer to the whole array - an array pointer. This is a different type which can be used to do pointer arithmetic on whole arrays, but apart from that it will still point at the very same address, where the array begins.

Comments

4

(&array,array and &array[0]) the same memory address.

Yes, but they differ in type.

  • &array is of type int (*) [3].
  • array is of type int [3] (Which at times, decay to a pointer to the first element)
  • &array[0] is of type int *.

By definition, the memory allocation for the array is contiguous, it starts at &array[0] and goes up to n-1 elements, n being the dimension.

If there is no location of "array" in RAM, where is it?

If by "RAM" you mean physical memory address, then your concept is wrong. Most of the cases, you are not using any physical memory directly, it's abstracted to you. The memory locations which get printed are from the virtual process address space allocated to your program.

Based on the storage duration, you can see the location varies, i.e., if the array has automatic storage, it'll reside on the function stack (call stack), it it has got static storage, probably it'll be on .bss section. Sometimes it might get optimized out also (not in this case, if left unused), so this depends on the environment. There's no guarantee of allocation unless compiler decided to allocate it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.