Home Forums C Programming pointers trivia

Viewing 1 reply thread
  • Author
    Posts
    • #2082
      Deepak
      Participant

      I have two questions right now in my mind :question – 1 :please have a look at this code :

      here…when p is passed to function, the thing that actually takes place is :a new variable recd is created and value of p is copied to recd (i mean a copy of p is created…consuming another 2 bytes)…now please check the following code :

      Does the same thing happen here?? i mean here too.. new pointer variable recd is created and value (an address) stored in pointer p is copied to recd???

      And here too, does the same thing happens???please throw some light on it… :)
      question – 2:

      In the above code, where the hell is 10 stored???what exactly happen in above code??my guess is : its stored in *p…i mean **p has a garbage which is taken as *p…and *p has a garbage which is taken as the address of 10….is this right or a garbage?? :P :)

    • #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…. 

Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.