6 Years
Discussion Span
Last Post by rhoit

Short answer: cout is interpreting the object as a bool due to the volatile qualifier. It's a quirk of overloading for the << operator.

Long answer: A volatile pointer can't be converted to a non-volatile pointer without an explicit cast, so neither the char* nor the void* overload can be used when the << operator is called. There's no volatile qualified overload, and the closest match is the bool overload, thus your array is interpreted as a boolean value rather than an address or a string.

You can fix it a number of ways, but an explicit cast is probably what you wanted:

std::cout<< (char*)str <<std::endl;

p.s. While your compiler may set uninitialized variables to 0 automatically, it's a very bad idea to rely on that behavior because it's not universal. So in your loop, int i; should be int i = 0; .

Edited by Narue: n/a


Thanx for explanation Narue! int i; was the slip! ;)

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.