Assuming your code is
int array[42] = {42};
int *ptr = array;
The compiler will reserve a space for the array . In case he knows that the array will have 42 positions for type int
(which is common to have 4 bytes, but depends on platform). This area of memory is somewhere in the stack 1 (which is relative to the position of the variable in the function, inside the stack frame ), has an address of it. So the compiler knows what this location is. He knows what this address is. When an operation requires this address, the compiler knows what to put there.
The second line causes array to decay for pointer. Then the address where array
is placed in ptr
.
Remember that we are talking about virtual memory. The actual physical memory address is calculated during execution.
1 In other cases it could be in heap