C++ char datatype array declaration error" array size not known" empty initializer
You are correct that the declaration char c[] alone will result in a compile error because the size of the array is not specified. The error message is "array size not known."
On the other hand, char c[]={}; is allowed and it means the same as char c[] which is array size is not known.
The reason for this is that the second example char c[]={}; is a special case in C++ called an "empty initializer."
It is a way to declare an array variable without specifying its size and at the same time, initializing it with an empty set of values.
In C++, an empty initializer is a set of curly braces with no elements inside, {}. This can be used to initialize objects of any type, including arrays, with their default values. And, this feature is not available in C.
In summary, char c[]={} is allowed as it is a special case of empty initialization, while char c[] is not allowed as there is no size provided to initialize the array.
Comments
Post a Comment