Home › Forums › C Programming › pointers trivia
- This topic has 1 reply, 2 voices, and was last updated 16 years, 7 months ago by Humayan.
- AuthorPosts
- March 20, 2008 at 9:48 am #2082DeepakParticipant
I have two questions right now in my mind :question – 1 :please have a look at this code :
123456789void function (int recd)<br />{<br />printf ("%d", recd);<br />}<br />void main()<br />{<br />int p=10;<br />function(p);<br />}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 :
12345678910void function (int *recd)<br />{<br />printf ("%d", *recd);<br />}<br />void main()<br />{<br />int *p;<br />*p=10;<br />function(p);<br />}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???
123456789void function (int *recd)<br />{<br />printf ("%d", *recd);<br />}<br />void main()<br />{<br />int p=10;<br />function(&p);<br />}And here too, does the same thing happens???please throw some light on it… :)
question – 2:12345void main()<br />{<br />int **p;<br />**p = 10;<br />}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 :)
- March 20, 2008 at 10:37 pm #3368HumayanParticipant
With this code:
123456789void function (int recd)<br />{<br />printf ("%d", recd);<br />}<br />void main()<br />{<br />int p=10;<br />function(p);<br />}
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:12345push bp ;push the base pointer<br />mov sp , bp ; save old stack pointer<br />sub sp , sizeOfData ; set pointer for variables needed<br />.......<br />mov [bp] + offset , p ;set value of parameter hereso 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:
1234567891011void function (int *recd)<br />{<br />printf ("%d", *recd);<br />}<br />void main()<br />{<br />int *p;<br />*p=10;<br />function ( p );<br />}<br />
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”:
123456void main()<br />{<br />int **p;<br />**p = 10; <br />}<br />
You have to intialize these too, like:
1234567<br />int a;<br />int * b;<br />int ** c;<br /><br />b = & a;<br />c = & b;
so that “c” is a pointer to a pointer ( c holds the address of b ). Now if we dereference “c” once:
1*c == the address of a
and twice :
1** c == the value held in aAnd 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….
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.