Reputation: 12175
Is the C++ compiler supposed to automatically insert the null character after the end of the char array? The following prints "Word". So does the C++ compiler automatically insert a null character at the last position in the array?
int main()
{
char x[] = {'W', 'o', 'r', 'd'};
cout << x << endl;
return 0;
}
Upvotes: 1
Views: 3219
Reputation: 349
Although it is not guaranteed, it seems my implementation always adds a \0
even if I am not requested.
I tested noNull[2]
is always \0
char noNull[] = {'H', 'i'};
for (int i = 2; i < 5; ++i) {
if (noNull[i] == '\0') {
std::cout << "index " << i << " is null" << std::endl;
} else {
std::cout << "index " << i << " is not null" << std::endl;
}
}
output
index 2 is null
index 3 is not null
index 4 is not null
Upvotes: 0
Reputation: 310930
In C/C++ string literals are automatically appended with terminating zero. So if you will write for example
char x[] = "Word";
then the size of the array will be equal to 5 and the last element of the array will contain '\0'.
When the following record is used as you showed
char x[] = {'W', 'o', 'r', 'd'};
then the compiler allocates exactly the same number of elements as the number of the initializers. That is in this case the array will have only 4 elements.
However if you would write the following way
char x[5] = {'W', 'o', 'r', 'd'};
then the fifth element will contain the terminating zero because it has no corresponding initializer and will be zero-initialized.
Also take into account that the following record is valid in C but invalid in C++
char x[4] = "Word";
Upvotes: 5
Reputation: 44438
No, it doesn't. If you want it inserted automatically, use a string constant:
const char *x = "Word";
//OR
std::string const s = "Word";
const char *x = s.c_str();
//OR
char x[] = { "Word" }; //using the C++11 brace initializer syntax.
The simplest way to verify this is to look at the memory address for x using a debugger and monitor it and the next few bytes following it. Stop the debugger after the initialization and examine the memory contents.
Upvotes: 1
Reputation: 78903
No, if you want to use the char
array as a string use this form of the initializer
char x[] = { "Word" };
Upvotes: 0
Reputation: 1849
No, in this case you are just creating a non-null terminated array. In-case of a read based on null termination. Your program won't stop as it wouldn't find the null at last.
char x[] = "Word";
In this case 5 bytes would be allocated for x. Null at the end.
Upvotes: 1
Reputation: 208323
That is undefined behavior, and it printed "Word" as it might also have crashed.
Upvotes: 3
Reputation: 439
No it doesn't. You yourself have to do that if you want to use it as a string or you want to use library functions for strings for that.
Upvotes: 1
Reputation: 227370
No, it doesn't. Your array x
has only 4 elements. You are passing std::cout
a pointer to char
, which is not the beginning of a null-terminated string. This is undefined behaviour.
As an example, on my platform, similar code printed Word?
, with a spurious question mark.
Upvotes: 6