简体   繁体   中英

C++ string input

My book says that if I want to read a string with a maximum length of 40 characters, I have to declare a variable with a length of 41, but it doesn't say why.

char name[41];
cin >> name;
cout << name << endl;

I know this is a newbie question and indeed I'm a C++ newbie.

I hope you can help me. Thanks.

EDIT:

Thanks for the answers, I didn't expect so much great information in such short time.

  1. Who is in charge of inserting the NULL terminator at the end of the char array?

  2. What happens with the "\\n" after I press Enter?

Thanks again.

Use a std::string to read from cin . Then you do not need to know in advance how big your buffer needs to be.

std::string input;
cin >> input;
cout << intput;

If you need a C-style array you can do:

const char* cstyle = input.c_str();

If you use a C-style string, the last character is always the null terminator '\\0' to indicate the end of the string. This is important to know where your sequence of characters ends.

Some example:

char* text = "hello"; // the compiler puts an extra '\0' at the end
std::string str("hello"); // does not have a null terminator! (before C++11)
str.c_str(); // this returns "hello\0" with a null terminator

There are two ways of declaring a string in C++:

char name[41];

or

string name;

One main difference between these is that there is always a \\0 character at the end of the character array to signify the end of it, so there is an extra cell required. Using string would be more convenient as you don't have to care about the length of the string. (And you can also use many build-in functions from the string library)

Check this out: Difference between string and char[] types in C++

The answer is that strings stored in character arrays, raw strings, are terminated with the null character ('\\0'). This control character signals the end of the string. This terminator is useful because the length of the string doesn't need to be stored explicitly. It's also useful because strings of any length that can fit in the array are allowed. In this situation, it's useful to have a marker for the end of the string. This question should prove useful in understanding the subject further.

A C-style string is an array of char s, the end of which is marked by a NUL terminator (ie a char having all its bits set to 0 ).

So, the string "Hi" looks like this in memory:

 +-----+-----+-----+ | H | i | NUL | +-----+-----+-----+

As you can note, a string of 2 characters ( "Hi" ) actually requires 3 (ie 2+1) characters, including the NUL terminator.

This is why a C-style string of 40 characters requires an array of 41 (ie 40+1) characters, because it must include room for the NUL terminator.


As a side note, I'd suggest using a robust and convenient string class in C++ (eg std::string ), instead of raw C-style arrays of characters with a NUL terminator.
If you use a string class, you'll make your code simpler, and you'll help yourself avoiding several potential security problems (eg buffer overruns) that are more likely to happen with raw C-style strings.

This would work just fine for string input in C++:

#include <iostream> // For std::cin/std::cout
#include <string>   // For std::string

....

std::string name;
std::cin >> name;                // Input
std::cout << name << std::endl;  // Output

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