Search code examples
c++netbeanscharcygwin

I can't understand char[] behavior in C++


Greeting to the Stack Overflow community. I'm trying to learn C++ from the very basics, and encountered some strange (to me at least) behavior. This is my "hello world"

#include <iostream>

int main() 
{
    std::cout << "Enter Thing! " << std::endl;
    char Thing[]="";
    std::cin >> Thing;
    std::cout << "Thing is " << Thing << "!" << std::endl;
    int i=0;
    while (i <= 10)
    {
        ++i;
         std::cout << "Thing is " << Thing << " in " << i << " while!" << std::endl; 
         std::cout << "i is " << i << "!" << std::endl;
    }
    std::cout << "Thing is " << Thing << " after while!" << std::endl;
    std::cout << "i is " << i << ".5!" << std::endl;
}

What actually bugs me is the output it gives.

Enter Thing!
thing
Thing is thing!
Thing is t☺ in 1 while!
i is 1!
Thing is t☻ in 2 while!
i is 2!
Thing is t♥ in 3 while!
i is 3!
Thing is t♦ in 4 while!
i is 4!
Thing is t♣ in 5 while!
i is 5!
Thing is t♠ in 6 while!
i is 6!
Thing is t in 7 while!
i is 7!
Thing is  in 8 while!
i is 8!
Thing is t       in 9 while!
i is 9!
Thing is t
 in 10 while!
i is 10!
Thing is t♂ in 11 while!
i is 11!
Thing is t♂ after while!
i is 11.5!

I don't really get what happens there. Even more, if i add "for" construction, for example

for (int i=4; i <=7; ++i)
    {
        std::cout << "i is " << i << "!" << std::endl;
        std::cout << "Thing is " << Thing << " for!" << std::endl;
    }
    std::cout << "i is " << i+3 << ".5!" << std::endl;    
    std::cout << "Thing is " << Thing << " after for!" << std::endl;

output changes to even more strange thing

Enter Thing!
thing
Thing is thing!
Thing is thing☺ in 1 while!
i is 1!
Thing is thing☻ in 2 while!
i is 2!
Thing is thing♥ in 3 while!
i is 3!
Thing is thing♦ in 4 while!
i is 4!
Thing is thing♣ in 5 while!
i is 5!
Thing is thing♠ in 6 while!
i is 6!
Thing is thing in 7 while!
i is 7!
Thing is thin in 8 while!
i is 8!
Thing is thing   in 9 while!
i is 9!
Thing is thing
 in 10 while!
i is 10!
Thing is thing♂ in 11 while!
i is 11!
Thing is thing♂ after while!
i is 11.5!
i is 4!
Thing is t♦ for!
i is 5!
Thing is t♣ for!
i is 6!
Thing is t♠ for!
i is 7!
Thing is t for!
i is 14.5!
Thing is  after for!

Probably it's me being stupid or something, but i can't progress on learning if i can't even make the most basic thing work. And sorry if the same question was already answered at some point, did my best to search for it with no success. So would you gladly point me to where am i being stupid? Thanks.

P.S. I'm under Win7x64m using NetBeans 8 with Cygwin.


Solution

  • The variable Thing is an array of only a single character. Writing more than one character to it will be writing out of bounds and lead to undefined behavior.

    The empty string literal "" is an array of a single character, the string terminator, and the compiler will use that to initialize the array Thing to be an exact copy.

    You basically have two solutions, either you declare the array to have a larger size:

    char Thing[128] = "";
    

    Or you use the standard C++ string class:

    std::string Thing;
    

    I definitely recommend the latter solution.