Hi! I've seen in some books notation like :
What does this do? Thanks!Code:a[14]={0}; char a[28]={'#'}
Hi! I've seen in some books notation like :
What does this do? Thanks!Code:a[14]={0}; char a[28]={'#'}
That does not work because you are redifining "a"; try it; you will end up with a compiler error. I have no idea why they would do that.
Unless those are 2 different lines of code (ie, in a different program). In that case, it probably fills the first element of the array with the charecter.
yeah they are two separate codes. So it only fill the first element?
Not quite. In the case of:So it only fill the first element?
the array is zero initialised, i.e. all 14 elements will be set to 0.Code:int a[14] = {0};
For the string:
the first character is set to '#', the rest are left as null characters.Code:char b[28] = {'#'};
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks for the help
Hi,
Just by way of further explanation: if you use an initializer list when you declare an array, i.e.:
and your initializer list does not contain the same number of elements as the size of the array, the remaining elements in the array will be automatically initialized to 0. 0 will be converted to the appropriate type for the array, e.g. a 0 for a char array is the '\0' character. So you can initialize a whole array to 0 doing this:Code:int nums[3] = {1, 2};
int char[20] = {0};
The first element is initialized to 0 or a '\0' character, and since the initializer list doesn't contain values for the rest of the array, the remaining elements are automatically initialized to '\0'. Now, if you do this:
char a[28]={'#'};
That assigns '#' to the first element in the array, and since the initializer list doesn't contain values for the rest of the array, the remaining elements are automatically initialized to '\0'.
Last edited by 7stud; 04-05-2006 at 01:46 PM.