The difference is that the "hello"
is a string literal and {'h', 'e', 'l', 'l', 'o', '\0'}
is a initializer list. The meaning of assigning a char*
to a string literal is to just make the pointer point at the string, while the meaning of assigning char*
to a initializer list is to just assign it to the first value (which is 'h'
converted to a char*
).
The proper way to use the initializer list is to use a char
array instead:
char s[] = {'h', 'e', 'l', 'l', 'o', '\0'};
but still there is a difference here as this way you should end up with a mutable buffer. Here the initializer list is used as a list of values to initialize the element in the array with. It's a shortcut for:
char s[6];
s[0] = 'h';
s[1] = 'e';
s[2] = 'l';
s[3] = 'l';
s[4] = 'o';
s[5] = '\0';