Why is typecasting an array of strings (initializer list) to its own type const char*[]
not allowed? Is it somehow secretly a different type? I also don't understand the compiler error as it says non-constant array expression
, but it is an initializer list with string literals, so that's definitely a constant expression.
Code:
const char* foo[] = (const char*[]){"t1", "t2", "t3", "t4"}; // Not OK
Output from GCC 11.4.0:
error: array initialized from non-constant array expression
22 | const char* foo[] = (const char*[]){"t1", "t2", "t3", "t4"}; // Not OK
| ^
Why is typecasting an array of strings (initializer list) to its own type const char*[]
not allowed? Is it somehow secretly a different type? I also don't understand the compiler error as it says non-constant array expression
, but it is an initializer list with string literals, so that's definitely a constant expression.
Code:
const char* foo[] = (const char*[]){"t1", "t2", "t3", "t4"}; // Not OK
Output from GCC 11.4.0:
error: array initialized from non-constant array expression
22 | const char* foo[] = (const char*[]){"t1", "t2", "t3", "t4"}; // Not OK
| ^
Share
edited Mar 7 at 13:59
Lundin
217k46 gold badges279 silver badges435 bronze badges
asked Mar 7 at 13:57
Qwert YuiopQwert Yuiop
1784 silver badges14 bronze badges
5
|
2 Answers
Reset to default 7Initializer lists don't have a type in C. It is a grammatical syntax item, not a type. So you can't "cast" them.
With the exception when an initializer is not an initializer list { ... }
but instead a string literal "..."
- a string literal has a type and in C that type is char[]
(as opposed to C++ where it is const char[]
).
What your code does is not casting. You are using a compound literal:
(const char*[]){"t1", "t2", "t3", "t4"};
This is another grammar item equivalent to "anonymous arrays" in other languages. It is an unnamed object with scope and storage duration, just like any variable. The part inside the parenthesis determines the type (const char*[])
and what follows is an initializer list. You can't have a compound literal without an initializer list.
So what your code is actually doing is trying to assign the (anonymous) compound literal array to the array foo
. But C doesn't allow assignment of arrays. Nor does it allow initialization of file scope variables declared outside a function when the initializers aren't constant expressions, which is what the compiler error is saying here.
The obvious solution is to skip the compound literal and just use an initializer list:
const char* foo[] = {"t1", "t2", "t3", "t4"};
This {"t1", "t2", "t3", "t4"}
has no type in C so you cannot cast it until you assign it to a variable of an actual type
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744925676a4601420.html
"t1"
ischar[3]
, notconst char*
. It might decay to a pointer, but that is not what it is. – BoP Commented Mar 7 at 14:06=
between the the declarator and the initializer list an assignment operator. – John Bollinger Commented Mar 7 at 14:49const char*
isn't the type of the data. – BoP Commented Mar 10 at 22:07{"John", "Co"}
, then the whole array is neitherconst char[][5]
norconst char[][3]
? So it doesn't have a type, what? – Qwert Yuiop Commented Mar 11 at 8:10