Pointers

int *p;
p=NULL;
cout<<"p :"<<p<<endl;

char *q;
q=0;
cout<<"q: "<<q<<endl;

string *t;
t=NULL;
cout<<"t: "<<t<<endl;

OUTPUT
p: 0
q: Press any key to continue...



Why the output is this? Can anyone tell me?
I want to ask that why the statement cout<<"t: "<<t<<endl; is not executed?
Remove

1
2
3
char *q;
 q=0;
 cout<<"q: "<<q<<endl;


and you will get two zeroes as a result of outputing. In fact your program has undefined behavior becuase it tries to access data at zero adsress in statement

cout<<"q: "<<q<<endl;

When you use a pointer of type char * in the operator << for stream std::cout it outputs data at the address stored in the pointer. So it doe not ouutput the value of the address itself but outputs data at this address.
closed account (zb0S216C)
q is a pointer to a char. Typically, a pointer to a char points to a string of characters. std::ostream overloads operator << to support char*, which effectively interprets the memory the pointer points to as a sequence of characters until the null-character ('\0') is reached. Since the null-character is equivalent to zero, nothing is printed.

Because of q, std::ostream fails to print the address of *t. As to why this happens, I'm not too sure.

Wazzak
Last edited on
q=0 means setting address of pointer to 0?

and why output of p is 0?It should be address like 0x8fb50000
q=0 means setting address of pointer to 0?


No, it does not. q=0 means give q the value zero.

You need to learn what a pointer is:
http://www.cplusplus.com/articles/EN3hAqkS/

and why output of p is 0?It should be address like 0x8fb50000
No, it should not. This cout<<"p :"<<p<<endl; outputs the value of p, NOT the location in memory where p is. What is the value of p? NULL. Which is zero. You're trying somethign too advanced for your level of understanding. You need to learn what a pointer is: http://www.cplusplus.com/articles/EN3hAqkS/
sorry but my compiler shows this output only and I have learnt this only in class.

we use *p or *q for values and p and q for address(in general term).
Last edited on
sorry but my compiler shows this output only

Your compiler is doing the right thing.

@Akshit
q=0 means setting address of pointer to 0?


The address of the pointer itself is not changed until it is alive. You assign a value to the pointer that is an address of some other memory or variable.
1
2
3
char *q;
q=0;
cout<<"q: "<<q<<endl;


The cout crashes my program. Something to do with:
streamsize _Count = (streamsize)_Traits::length(_Val); // may overflow
Last edited on
The cout crashes my program.


Well let's take a look at the code.

char *q;
So, q is a char pointer. What value does it have? You didn't set it yet, so it could be any value at all, thus it could be pointing anywhere in memory at all.

q=0;

Now you set the value of the pointer to zero. The pointer is pointing at memory address zero. This is commonly known as a null pointer; it is a special memory address used to indicate that the pointer is deliberately not pointing at anything. In C++11, we now have the nullptr keyword for this.

cout<<"q: "<<q<<endl;
Now, we pass the pointer to cout, using the << operator, which is cleverly overloaded to dela with char pointers. In this case, the result is: go to the address is points to, output that as a char, and output all the following addresses as a char until you get to a zero value, and then stop. You have thus passed a pointer that deliberately doesn't point at anything to cout. What happens when cout tries to read that memory address to output its contents as a char? Well, you can see for yourself in this case - a crash. I'd normally expect a segFault if you tried to dereference a null pointer yourself. Exactly what happens when you feed it to the << operator is not something I'd like to guess.

However, I would have expected it to deal with it elegantly. Using g++ 4.7.0 and the version of clang++ I have on my machine, it is dealt with elegantly without crashing - what compiler are you using?
Last edited on
what compiler are you using?

Good Question. I guess the VS 2010 default.

Maybe this is an answer:
/Zi /nologo /W3 /WX- /O2 /Oi /Oy- /GL /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm- /EHsc /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Release\Fun.pch" /Fa"Release\" /Fo"Release\" /Fd"Release\vc100.pdb" /Gd /analyze- /errorReport:queue


I see a few options there that are listed here:
http://msdn.microsoft.com/en-us/library/fwkeyyhe
Topic archived. No new replies allowed.