While reading this question I wanted to test the input in GCC to see what errors would be output. To my surprise the following line:
char array[] = {"s"};
compiles without error or warning, resulting in an array of size 2 containing "s\0"
. I would have expected a compiler error because the right side of the expression is of type char*[]
.
Is an array initialization with only one element not treated as an array in this case, and why?
char array[] = {"s"};
is same as:
char array[] = "s";
Here { }
are optional in this case because "s" is string literal.
Or,
char array[] = {'s', '\0'};
In this case, { }
are necessary to initialize the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With