c - Why is typecasting an array of strings (initializer list) to its own type const char*[] not allowed? - Stack Overflow

Why is typecasting an array of strings (initializer list) to its own type const char*[] not allowed? Is

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
  • 1 The type of "t1" is char[3], not const char*. It might decay to a pointer, but that is not what it is. – BoP Commented Mar 7 at 14:06
  • Initializer lists are not expressions at all, of any type, though their elements are expressions. Nor is the = between the the declarator and the initializer list an assignment operator. – John Bollinger Commented Mar 7 at 14:49
  • @BoP, in my example the strings happen to be the same size, but in the general case they don't have to be. So does that mean this initializer list does not generally have a type? – Qwert Yuiop Commented Mar 10 at 15:33
  • @qwert - My comment was only about "typecasting to its own type", and that the code doesn't do that, because const char* isn't the type of the data. – BoP Commented Mar 10 at 22:07
  • @BoP, no what I mean is if I make an initializer list with different string sizes i.e. {"John", "Co"}, then the whole array is neither const char[][5] nor const char[][3] ? So it doesn't have a type, what? – Qwert Yuiop Commented Mar 11 at 8:10
Add a comment  | 

2 Answers 2

Reset to default 7

Initializer 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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信