Can someone explain this excerpt from a book I'm studying:

char phrase[] = "Game Over!!!";

BOOK:
"C-style strings terminate with a character called the null character to signify
their end. You can write the null character as ’\0’. I didn’t need to use the null
character in the previous code because it is stored at the end of the string for me."

Question 1:
It says the null character is stored at the end of the string. Is that what the ";" operator is doing? Where is the null character being stored at the end?

Question 2:
If phrase is a CHAR type, why are there quotations around it? Is that what's turning it into a string?

Buddy you need to :
1. get a pure c++ beginner book
2. read about basic types
3. then do it 2. more types until you get it what is char

Becouse it is broad topic to discuss, and your questions are not clear and even you dont know what you are asking.

Q1
The semicolon (;) just indicates the end of a statement. It has nothing to do with the end of the string.
When you use double quotes (") around text, your compiler kindly puts a null character at the end.

Q2
phrase is an array of 13 chars. This -> char phrase[] = "Game Over!!!"; is interpreted by the compiler as char phrase[13] = { 'G', 'a', 'm', 'e', ' ', 'O', 'v', 'e', 'r', '!', '!', '!', '\0' };

commented: Very Helpful +3

1. The null terminator is a character at the end of the string that contains '\0'. The example you posted could be rewritten like this:

char phrase[13] = {'G','a','m','e',' ','O','v','e','r','!','!','!',','\0'}; Its just a lot more convenient for us to type it as in your example, and the compiler will generate the correct code to null terminate the string.

2. You mean the quotes around [b] "Game Over!!!"[/b]? Yes the quotes are required so that the compiler knows where the beginning and end of the string are.

[edit]^^^ Oops he posted before I did.[icode]
char phrase[13] = {'G','a','m','e',' ','O','v','e','r','!','!','!',','\0'};
Its just a lot more convenient for us to type it as in your example, and the compiler will generate the correct code to null terminate the string.

2. You mean the quotes around "Game Over!!!"? Yes the quotes are required so that the compiler knows where the beginning and end of the string are.


[edit]^^^ Oops he posted before I did.

commented: awesome +3

Just one more things, for question 2, its not a char type, its a array to chars, which is why in the above post, you see an array of chars

Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.