0

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?

5
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by crapgarden
Featured Replies
  • [B]Q1[/B] 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. [B]Q2[/B] phrase is an array of 13 chars. This -> … Read More

  • 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: [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 … Read More

  • 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 Read More

-1

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.

1

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' };

Edited by m4ster_r0shi: n/a

Comments
Very Helpful
1

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.

Edited by Ancient Dragon: n/a

Comments
awesome
1

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.