简体   繁体   中英

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.

PS I'm under Win7x64m using NetBeans 8 with Cygwin.

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.

This is problematic:

 char Thing[]="";

You're creating an empty array of single char, and write the user input into it.

You have the correct output by chance for the first time, but then the data is overwritten and you get this weird output.

Create the string with sufficient buffer length

char thing[256];  

or use the string object.

As the others answers suggest, you use your char array without really initialising it (supplying a fixed size). Furthermore this is C-style programming. I highly suggest using C++'s std::string instead of a char -Array, as this won't be so error prone.

Size of such array declared locally should be fixed since the code is compiled.

char Thing[] = "";

This gives you an illusion that size of 'Thing' is variable. Sadlly, size of 'Thing' will be automatically deduced according to the initial value given on the right, which makes the string you input written on an unexpected position in the memory, causing undefined behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM