Consider the following program:#includemain(){int i=3;int *j;int **k;j=&i;k=&j;printf("Address of i=%un",&i);printf("Address of j=%un",&j);printf("Address of k=%un",&k);return 0;}Suppose the answer is:Address of i=2004Address of j=2002Address of k=2000why is the address decreasing ?

Showing Answers 1 - 6 of 6 Answers

pvsola

  • Mar 12th, 2008
 

Explanation:

Local variables and argument passed are stored in reverse order in called function stack, therefore first local variables will be stored and then argument passed. Now the last variable in this code is int** k; after storing value of k in the stack, stack increase by the amount of the datatype declared. i.e. LSB of K is stored in 2000 and MSB is stored in 2001 (of-course processor dependent for being big-endian or little endian) -> so here value is 0x0003. so next address is 2002-> which stores value of int *j; and similarly 2004->int i.

Also most processor increment and decrement stack by word, therefore char variable will consume 2 byte. So be carefull while assigning stack value.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions