0

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 .

Edited by Efficience: n/a

2
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by Efficience
0

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;

Edited by gerard4143: n/a

0

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.

0

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

Edited by gerard4143: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.