C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 5)
5.
What will be the output of the program If the integer is 4bytes long?
#include<stdio.h>

int main()
{
    int ***r, **q, *p, i=8;
    p = &i;
    q = &p;
    r = &q;
    printf("%d, %d, %d\n", *p, **q, ***r);
    return 0;
}
8, 8, 8
4000, 4002, 4004
4000, 4004, 4008
4000, 4008, 4016
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
35 comments Page 2 of 4.

Perumalsamy said:   1 decade ago
First assume i=8
and then
p =&i; // p holds address of i
q=&p; // q holds address of p
r=&q; // and r holds address of q
*p means value of i;
**q and ***r and is called chain pointer that means
p<-q<-r

So all value is 8.

Uttam said:   1 decade ago
Let the addresses of variables i,p,q,r are 0x100,0x200,0x300,0x400 respectively.

Here p,q,r are pointers.

p holds address of i.ie 0x100.

q holds address of p.ie 0x200.

r holds address of q.ie 0x300.


so we can say r is the pointer which indirectly refers to the address of variable i.

so value that pointer r holds is indirectly equal to the value of i.

Madhu said:   1 decade ago
If it declaired that int i,*p then
p= &i (it means p stores the address of i)
*p=value at i
same way
q= &p
**q=value at &i(it means pointer to a integer pointer)
***r= value at &i(it means pointer to a pointer to a poiter )
and it has no limit to extend this pointer to pointer so we can use number of' *'.but all are print only the value so it prints 8,8,8.

Zohra said:   1 decade ago
Well explained. Sunil.

Tarun said:   1 decade ago
@Jyothi. Explain nicely thanks for understand me.

Shashank sinha said:   1 decade ago
Thanx Jyoti

Ramya said:   1 decade ago
Here r contains ***r so it point the i pointer variable.

And q contain the ** pointer variable. And p contain the * pointer variable so it point the i pointer address value.

Sumallika said:   1 decade ago
p=&i
*p=*(&i)=i
so *p=8;

q=&p,
*q=*(&p)=p,
**q=*p,
**q=8;

r=&q,
*r=*(&q)=q
**r=*q,
***r=**q,
***r=8

Manjiri shastri said:   10 years ago
Can you please tell me how it work? I mean how pointer storing the address?

Harendra gotla said:   9 years ago
Thank you @Sumallika, the way you solve the problem is clever and easy to understand.


Post your comments here:

Your comments will be displayed after verification.