1

I was hoping that I could get some further explanation. I was told that I need to explicitly add \0 to the end of a string. Apparently this is for the C++ string class and that it is actually an array of characters that seems to be parsed under the hood. I was told that we must use the \0 in order to tell where the end of the string is as seen below:

int main() { char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; cout << str << endl; return 0; } 

However, if I have a user input their name, for example, I don't believe that C++ automatically uses the \0 to terminate the string. So the argument that the \0 must be there to know where the string ends makes no sense. Why cant we use the .length() function to account for the length of the string?

I wrote the following program to illustrate that the length of the input can be found from the .length() function.

int main() { string firstName; cout << "Enter your first name: "; cin >> firstName; cout << "First Name = " << firstName << endl; cout << "String Length = " << firstName.length() << endl; return 0; } 

So, if the user inputs the name "Tom". Then the output would be the following:

First Name = Tom String Length = 3 

I brought this to my professor's attention and also this article http://www.cplusplus.com/reference/string/string/length/ and I was told that is why I am in college because it cannot be done this way. Can any one offer any insight, since I don't understand what I am missing?

2
  • 2
    You're confusing char[] and "quoted string literals" and the string class willy-nilly here. Please clarify your actual question. The purpose of the loop escapes me completely, as does what your output is supposed to mean. Commented Nov 5, 2014 at 0:57
  • You're confusing std::string with C strings. ` Why cant we use the .length() function to account for the length of the string.` How do you think std::string:length knows the length of the string? Commented Nov 5, 2014 at 0:58

2 Answers 2

2

The "C string" was adopted into C++ from the C language. The C language did not have a string type. Strings in C were represented as an array of char, and the string was terminated with the NUL byte (\0). A plain string literal in C++ still has these semantics.

The C++ string type maintains the length within the object, as you say, so in a string, the NUL is not required. To get a "C string" from a string, you can use the c_str() method on the string. This is useful if you need to pass the contents of the C++ string to a function that only understands the NUL terminated variety.

std::string s("a string"); // s is initialized, // the length is computed when \0 is encountered. assert(s.size() == sizeof("a string")-1); // sizeof string literal includes the \0 assert(s.c_str()[s.size()] == '\0'); // c_str() includes the \0 

In your first program, you are initializing an array of char with an initializer list. The initialization is equivalent to the following:

char str[6] = "Hello"; 

This style of initializing an array of char is a special allowance that C++ provides since it is the syntax accepted by C.

In your second program, you are getting the name from the standard input. When C++ scans the input to populate the string argument, it essentially scans byte by byte until it encounters a separator (whitespace characters, by default). It may or may not insert a NUL byte at the end.

Sign up to request clarification or add additional context in comments.

Comments

0

You're not missing anything per se. The null terminator is used on character arrays to indicate the end. However, the string class takes care of all of that for you. The length attribute is a perfectly acceptable way of doing it since you're using strings.

However, if you're using a character array, then yes, you would need to check if you're on the null terminator, as you may not know the length of your string.

The following will give you no issues.

int length = 2; char str[] = "AB"; 

However, try the following, and you'll see some issues.

int length = 5; char str[length + 1] = "ABCDE"; // +1 makes room for automatic \0 char str2[length + 1] = "ABC"; 

Try the second snipped using your for loop method knowing the length, and the first one will give you ABCDE, but the second one will give you "ABC" followed by one junk character. It's only one because you'll have [A][B][C][\0][JUNK] in your array. Make length larger and you'll see more junk.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.