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?
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?
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?
Yes, but more accurately, assuming that the array contains the longest possible string that it can contain.Originally Posted by DickArmy
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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 ....
will be terminated by a zero.Code:char array[10] = "123456789";
Will NOT be terminated, as it fills the 10 characters of the array, and there is no space for a terminating zero.Code:char array[10] = "123456789A";
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
thanks for the help... <3
@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.
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
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.