# Terminating Zero

• 06-30-2009
DickArmy
Terminating Zero
I have a question about termination zero '\0' in an array of characters. Lets say I create:

char array[10];

My Question is: Is the will the terminating zero be at array[9]? Or is there memory for 10 chars, index 0-9?
• 06-30-2009
laserlight
The array would not have been initialised, so there may not even be a null character in the array. There will be enough memory for 10 characters, which means that the array can be used to store a null terminated string of up to a length of 9.
• 06-30-2009
DickArmy
Right, should of given example with an initialized char array. Assuming then that the array is full of characters, the terminating zero will be at the last index and only 9 non '\0' chars will be stored. Is this correct?
• 06-30-2009
laserlight
Quote:

Originally Posted by DickArmy
Assuming then that the array is full of characters, the terminating zero will be at the last index and only 9 non '\0' chars will be stored. Is this correct?

Yes, but more accurately, assuming that the array contains the longest possible string that it can contain.
• 06-30-2009
tabstop
Quote:

Originally Posted by DickArmy
Right, should of given example with an initialized char array. Assuming then that the array is full of characters, the terminating zero will be at the last index and only 9 non '\0' chars will be stored. Is this correct?

That depends entirely on what initializer you give. If you give for instance "hello", you'll get 'h', 'e', 'l', 'l', 'o', '\0', '\0', '\0', '\0', '\0'. If you give an initializer that's too long, the first ten characters should be placed in the array, and what happens to the rest ....
• 06-30-2009
matsp
Code:

`char array[10] = "123456789";`
will be terminated by a zero.

Code:

`char array[10] = "123456789A";`
Will NOT be terminated, as it fills the 10 characters of the array, and there is no space for a terminating zero.

If the initializing value is shorter than 9 characters, the terminating zero will be before the [9] position, but the remainder of the space in the array will be zero as well.

--
Mats
• 06-30-2009
DickArmy
thanks for the help... <3
• 06-30-2009
BEN10
@dickarmy
Initializing more elements than the size of the array is an error except for char where it's not. Although C allows it but C++ throws an error.
• 07-01-2009
linuxdude
Quote:

Originally Posted by tabstop
If you give for instance "hello", you'll get 'h', 'e', 'l', 'l', 'o', '\0', '\0', '\0', '\0', '\0'.

Are you sure that is the standard? I'm not sure, but I can't find anything saying whether it will be filled with \0's or just be 'h', 'e', 'l', 'l', 'o', '\0', ????
I always thought it was the latter, but I'm not sure. A quick gcc tests fills the array with \0's, but I'm not sure if that is just them being nice or standard.
• 07-01-2009
tabstop
Quote:

Originally Posted by linuxdude
Are you sure that is the standard? I'm not sure, but I can't find anything saying whether it will be filled with \0's or just be 'h', 'e', 'l', 'l', 'o', '\0', ????
I always thought it was the latter, but I'm not sure. A quick gcc tests fills the array with \0's, but I'm not sure if that is just them being nice or standard.

The standard states that if you use an initializer that is short (contains fewer elements than the array), the remainder of the array will be zero-initialized. (Think
Code:

`int bob[50] = {0}`
.)