Home Forums C Programming pointers trivia Reply To: pointers trivia

#3368
Humayan
Participant

With this code:
 

 
The compiler will push the parameter p onto the system stack and jump to the code for the function “function”. Inside the code for “function” a activation record is created which sets up memory for the actual parameters of function and other space for local and temporary variables needed by the compilers code generation algorithms. For intel hardware the code might look something like ( assembly language ):
 
push p ; push the variable call function
 
function:

so that is how the memory might be set up ( look at the assembly output of your compiler if you want to see how any feature is translated ).
 
For the second example:
 

 
memory on the stack is set up for a pointer ( in this case you code should could crash since p is not initialized ) to an int ( 32 bits on a 32 bit system ) and the memory is initialized with the address of p. It is renamed recd and dereferenced to get the value 10. Code should be:
 
   int z;
   …
   p = & z;
 
On the third function the pointer parameter is simply initialized with the value of the “p” variable address and then dereferenced. In the last example you have a “pointer-pointer”:
 

 
You have to intialize these too, like:
 

 
so that “c” is a pointer to a pointer ( c holds the address of b ). Now if we dereference “c” once:
 

 
and twice :
 

And this is stored on the stack in the same way as the others. I can write some example code and get you some assembly outputs if you need them….