C language problems

char str[] = "abcdef";
char &r = str[0]; // R is a reference
cout <<&r <<endl; // abcdef

char str1[] = "abcdef";
char r1 = str1[0];
cout <<r1 <<endl;
cout <<&r1 <<endl;  // Why the output: a hot Yin bcdef

Started by Pete at November 13, 2016 - 6:23 PM

This results in pure CIS coincidence, don't study this problem´╝ü

ps: Cout the best printing information into printf format output, with good

Posted by Kelly at November 28, 2016 - 6:37 PM

Hello, I use printf to the same output, don't know why?

Posted by Pete at December 13, 2016 - 7:21 PM

Local variables stack sequence, C style string definition, ANSI coding, automatic filling 0xCC in Debug mode, VC, these add up to get this result
From the language level, this code is wrong is the pointer by output string output to a character
From the implementation mechanism, want to know why this happened, go to understand listed above it, give you a hint, "a hot Yin bcdef" in memory is "a 0xCC 0xCC 0xCC"."abcdef"

Posted by Estelle at December 19, 2016 - 8:00 PM

1)A character is not a string
2)A character pointer, can point to a single character, can also refer to a character array, string, point to the first character.
3)C++ ostream operator<<The output character of the pointer, as the output string processing.
Although the printf output the same effect,
But printf%p-->%s--->%c pointer, string, character, integer%d, one one corresponding, but will not be in order.,
Casually use exception.

Posted by dream at January 01, 2017 - 8:52 PM