相关疑难解决方法(0)

字符串数组初始化

这是我的另一个问题的延续.

请考虑以下代码:

char *hi = "hello";

char *array1[3] = 
{
    hi,
    "world",
    "there."
};
Run Code Online (Sandbox Code Playgroud)

它没有编译到我的意外(显然我不知道C语法以及我认为)并生成以下错误:

  error: initializer element is not constant
Run Code Online (Sandbox Code Playgroud)

如果我将char*更改为char [],则编译正常:

char hi[] = "hello";

char *array1[3] = 
{
    hi,
    "world",
    "there."
};
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释原因吗?

c string pointers ansi-c

10
推荐指数
1
解决办法
284
查看次数

标签 统计

ansi-c ×1

c ×1

pointers ×1

string ×1