-1

The title almost says it all but: 1. I am reading a tutorial about character sequences and encountered a section that I do not really understand. They initialize an array of characters with a null character at the end like so:

char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' };

However, they do not really explain anything further about the null character(I have also tried to use google but sadly I did not find the answer I want). The tutorial says

"By convention, the end of strings represented in character sequences is signaled by a special character: the null character"

which to me means "Most of the times but not every time, a null character is used." So my first question is - When is the null character being used and when do you know if you have to input it explicitly?

2.. I have this code:

char foo[22] = { 'H', 'e', 'l', 'l', 'o' };
cout << foo << endl;

Where the output is: Hello

But this is without me adding the null character and in the tutorial, they initialize the the array with a null character at the end as I showed previously even though that the output of both examples is the exact same?? :/

So my second question is - What is it actually that the null character do?

Thanks, your time is much appreciated!

That Guy
  • 2,349
  • 2
  • 12
  • 18
  • 3
    `char foo[22] = { 'H', 'e', 'l', 'l', 'o' };` has 17 `'\0'` at the end. – mch Feb 16 '17 at 14:06
  • 7
    These kind of questions are better answered by a [book](http://stackoverflow.com/a/388282/3484570). – nwp Feb 16 '17 at 14:07
  • In short, in c/c++, normally you have to specify the length of an array when using it (think about passing around an int[]). char array has a short cut using `'\0'` at the end to avoid the need for the length. `char` type is a byte and the null character's value is 0. So when char array is allocated, it is **likely** everything is a null character to start with. But this is not guaranteed. Thus a good habit to explicitly specify a null at the end. – greedy52 Feb 16 '17 at 14:14
  • a C string always end by a `'\0'` – phuclv Feb 16 '17 at 14:21
  • @XinHuang: *"So when char array is allocated, it is likely everything is a null character to start with."* - The C++ standard makes no such claims. If you allocate an array, its contents are indeterminate. – IInspectable Feb 16 '17 at 14:33
  • @IInspectable and yes, you are correct. Forgive my wording. Array allocation gets whatever is left in the memory. 'likely' -> I meant it is possible. Maybe 'not very likely' in this is more appropriate. Only static arrays are initialized to 0. – greedy52 Feb 16 '17 at 14:42
  • @nwp: These questions _should_ be answered by a book or tutorial, but apparently the tutorial mentioned in the first paragraph fails to address it. Clarifying an unclear part of a tutorial can definitely be on-topic here. – MSalters Feb 16 '17 at 14:54
  • Thanks guys, I will read it all through in just a moment and compare. You are the best :) – That Guy Feb 16 '17 at 15:42

2 Answers2

4

'\0' is the null termination character. Without this you won't be able to know when the strings end. In your example

char foo[22] = { 'H', 'e', 'l', 'l', 'o' };

Is the same as

char foo[22] = { 'H', 'e', 'l', 'l', 'o', '\0', '\0', ..., '\0' };

With 17 times '\0'. When you Output the string you could have something like:

auto i = 0
do
{
    // Do smoething with foo[i++]
}
while('\0' != foo[i]);
YSC
  • 38,212
  • 9
  • 96
  • 149
  • You could develop about all C `str...()` functions using the trailing nul character to compute the C-style string length or stop its treatment. You may also give an alternative of the usage of the nul char, for instance in the pascal-style string (used by `std::string`). – YSC Feb 16 '17 at 14:15
  • 2
    Your `do`-`while` loop should really be a `while` loop. And it's worth noting, when and why the compiler fills an array with an explicit size with the equivalent of NUL characters. – IInspectable Feb 16 '17 at 14:30
1

When is the null character being used

Whenever a null terminated character string is needed. When do I need a null terminated character string, you might ask. Well, whenever you call a function that requires a null terminated character string. Whether a function requires a null terminated character string or not can be found from the documentation of the function. Examples of such functions are: std::strlen(const char*), operator<<(std::ostream&, const char*) and countless others.

when do you know if you have to input it explicitly

Whenever you need a null terminated character string and you either know that your character array would not otherwise be null terminated or you don't know whether it would be.

even though that the output of both examples is the exact same??

If you were to insert into an output stream a pointer to a character array that is not null terminated, the behaviour of the program would be undefined.

However, your character array contains nulls after the characters that you initialized explicitly. This is because value initialization sets characters without an explicit initializer to null.

What is it actually that the null character do?

It designates the end of a null terminated character string.


aren't all character sequences a null terminated character sequence then, since they all need that null character to indicate the end

If a character sequence ends in a null terminating character, then it is null terminated by definition. You can use such sequence as an argument to standard functions like std::strlen.

But no: A character sequence doesn't necessarily contain a null character. A character sequence that doesn't contain a null character is not null terminated by definition, and must not be used as an argument to functions that expect a null terminated string. An example:

char non_terminated[] = {'h', 'i', '!'};
auto len = std::strlen(non_terminated); // oops, UB!
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Thanks buddy and of course also to the others, I will investigate your answer more carefully in a moment but they also that this "the result is: " is a string literal - but isn't a string literal and string constant the same? Not that I need a very detailed explanation with examples etc. but because I do not really think that it is worth starting a new thread for... Thanks guys! – That Guy Feb 16 '17 at 15:39
  • @npp yes, the C++ standard mentions no such thing as a string constant, but I believe it is a colloquial synonym for string literal. – eerorika Feb 16 '17 at 15:48
  • Okay then, thanks! So you would not really mind whether to use string literal or string constant, correct? – That Guy Feb 16 '17 at 15:54
  • @npp I recommend using *literal*, because it is official and unambiguous. But it is also important to know that *constant* can mean the same, to understand others. – eerorika Feb 16 '17 at 16:02
  • Thanks! But you say "Well, whenever you call a function that requires a null terminated character string. Whether a function requires a null terminated character string" and aren't all character sequences a null terminated character sequence then, since they all need that null character to indicate the end? and the same with the type strings correct? Because they, in fact, are null terminated arrays of characters. Or am I wrong here? – That Guy Feb 16 '17 at 20:17
  • @npp see the edit. I don't quite understand rest of your comment. What is a "type string"? – eerorika Feb 16 '17 at 21:32
  • Thank you and I meant class string from the standard library – That Guy Feb 16 '17 at 22:58