General discussion

Locked

some problem with pointers in c++

By monukaushal ·
main()
{
int x=10,*p,*q;
p=&x;
q=p+6;
cout<<"Contents of q is"<<*q;
}
my question is y the answer is 0 as the value must be an address.

This conversation is currently closed to new comments.

1 total post (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by PubServant In reply to some problem with pointer ...

You have dereferenced q in the cout statement. By using *q, you have asked that the value at storage location q be printed. The contents may be 0 as you have stated, or it more typically is a residual value at that storage location since variables aren't initialized when they are declared.

If you want the address to be printed, use q without the "*" in the cout statement as in the following statement.

cout << "The address of q is " << q;

Back to Web Development Forum
1 total post (Page 1 of 1)  

Related Discussions

Related Forums