Joseph Hesse <
jo...@gmail.com> wrote:
> char x[] = {"Hello"}; // x is a 0 terminated character array
To give a slightly different answer as others, which might give
some insight, please note that
char x[] = "Hello";
is a rather different thing than
char* x = "Hello";
(Technically that's not completely kosher because it should be
a const pointer, but that's not really the point.)
The former is a built-in array, which is being initialized with
the values in that string literal. The latter is a char pointer
that's initialized to point to that string literal.
Normally built-in arrays (which don't have a specified size
inside the brackets) would be initialized like:
int x[] = { 1, 2, 3, 4 };
That would be a built-in array with 4 elements.
There exists, however, a special syntax for char arrays, which
allows them to be initialized (and their size determined) with
a string literal:
char x[] = "Hello";
This is syntactic sugar that's equivalent to:
char x[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
When you create a range-based for using that array, it will
see that it's an array of 6 elements, and will iterate through
those 6 elements.