Assuming your code is
int array[42] = {42};
int *ptr = array;
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 memory area is somewhere in the stack* (which relates to the position of the variable in the function, within the stack frame), has an address for 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 the decay of array for pointer. Then the address where it is array
is placed in ptr
.
Remember that we are talking about virtual memory. The actual physical address of the memory is calculated while running.
*In other cases it could be in heap, this is implementation detail too, but for all intents and purposes it is so.
There’s nothing wrong with your code?
– Maniero
I do not know, the code is not mine. I withdrew the answer of the question as I said, but I did not understand the functioning of it.
– Marcelo de Andrade