Hello Everyone ,
I've this strange problem with using char pointer in my c++ programme with the gcc compiler (version 3.4.2).

char a='A';
    char *c=&a;
    printf("%p   %c\n",c,*c);
    std::cout<<c<<"     "<<*c<<std::endl;

I expect the output of printf and cout to be same but why am I getting strange output like this.
output :-
0022FF27 A
Ah " A

Now If I change char a='A' with char a='C'
output is :-
0022FF27 C
Ch " C

Is their anyone who can explain this. Thank you a million in advance. Please help me .

std::cout is interpreting c as a character string...You know something that ends with a '\0'.

If you want the address try this:

std::cout<<(void*)c<<"     "<<*c<<std::endl;

Hmm thanx gerard I get it . But why their is an extra h with quotes in the output.
If i write char *c="hello" . Their is no extra h and " in the output.
Can you please give the reason for this.Thank a lot in advance.

Hmm thanx gerard I get it . But why their is an extra h with quotes in the output.
If i write char *c="hello" . Their is no extra h and " in the output.
Can you please give the reason for this.Thank a lot in advance.

When you call c with std::cout << c << std::endl; it will try to keep reading/displaying until it finds a '\0'. Why is it displaying the extra 'h'? Its probably the next printable character it found....On my system it printed a whole string of nonsense before it terminated with a '\0'.

Here's my output

0x7fff8fbb9faf A
A����� A

Hmm .. Now I understand it compleatly .. thank you gerard ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.