#include <stdio.h> int main() { char a = 5; char b[2] = "hi"; // No explicit room for `\0`. char c = 6; return 0; }
Whenever we write a string, enclosed in double quotes, C automatically creates an array of characters for us, containing that string, terminated by the \0 character http://www.eskimo.com/~scs/cclass/notes/sx8.html
In the above example b
only has room for 2 characters so the null terminating char doesn't have a spot to be placed at and yet the compiler is reorganizing the memory store instructions so that a
and c
are stored before b
in memory to make room for a \0
at the end of the array.
Is this expected or am I hitting undefined behavior?
There isn't a character that is reserved, so you must be careful not to fill the entire array to the point it can't be null terminated. Char functions rely on the null terminator, and you will get disastrous results from them if you find yourself in the situation you describe.
You can't initialise a char array with NULL , arrays can never be NULL .
Null-terminated strings are a C thing, and only that. Other languages are free to work with strings as they see fit (e.g. preceded by a length field, $ terminated, etc.) An array in Java is just that - a collection of same-typed objects. There is no special treatment given for char arrays.
They are not terminated by the null pointer NULL , which is a completely different kind of value with a completely different purpose. NUL is guaranteed to have the integer value zero. Within the string, it will also have the size of the underlying character type, which will usually be 1.
It is allowed to initialize a char
array with a string if the array is at least large enough to hold all of the characters in the string besides the null terminator.
This is detailed in section 6.7.9p14 of the C standard:
An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
However, this also means that you can't treat the array as a string since it's not null terminated. So as written, since you're not performing any string operations on b
, your code is fine.
What you can't do is initialize with a string that's too long, i.e.:
char b[2] = "hello";
As this gives more initializers than can fit in the array and is a constraint violation. Section 6.7.9p2 states this as follows:
No initializer shall attempt to provide a value for an object not contained within the entity being initialized.
If you were to declare and initialize the array like this:
char b[] = "hi";
Then b
would be an array of size 3, which is large enough to hold the two characters in the string constant plus the terminating null byte, making b
a string.
To summarize:
If the array has a fixed size:
If the array does not have an explicit size, the array will be sized to hold the string constant plus the terminating null byte.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With