Discussion Locked

some problem with pointers in c++

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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.

1 reply

by pubservant

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;

This discussion is closed to new replies.