简体   繁体   English

在初始化指针数组时,为什么只有字符串不显示警告?

[英]In initializing pointer array, why only string doesn't show warning?

I'm not making a specific program.我不是在制作特定的程序。 I was curious about some kind of syntax things in C language.我很好奇 C 语言中的某种语法。
I made these declarations.我做了这些声明。

int main()
{
    char *titles[] = {"NUDGE", "DECOUPLEING", "WORLD WAR Z"};
    char *letters[] = {{'a', 'b', 'c'}, {'x', 'y', 'z'}};
}

In the second declaration, there were 3 kinds of warnings.在第二次声明中,有3种警告。

  1. braces around scalar initializer标量初始值设定项周围的大括号
  2. initialization makes pointer from integer without a cast初始化使指针来自 integer 而无需强制转换
  3. excess elements in scalar initializer标量初始值设定项中的多余元素

I got that *numbers[] kind of things should have pointer values in it.我知道*numbers[]之类的东西应该有指针值。
Is the first declaration doesn't show warning because string type is a pointer?第一个声明不显示警告是因为字符串类型是指针吗?
and character is not a string?字符不是字符串?

+) What does 'scalar initializer' means in first, and third warning? +) 第一次和第三次警告中的“标量初始化程序”是什么意思?

+) +)

char titles[][] = {"NUDGE", "DECOUPLEING", "WORLD WAR Z"};

What this shows error and the first one doesn't show error?这是什么显示错误而第一个不显示错误?


I'm a beginner at C pointer.我是 C 指针的初学者。 Please explain:)请解释:)

the second initialization is invalid as {'a', 'b', 'c'} is not the array which can dacal to pointer第二次初始化无效,因为{'a', 'b', 'c'}不是可以指向指针的数组

you should use compound literals instead:您应该改用复合文字:

char *letters[] = {(char[]){'a', 'b', 'c'}, (char []){'x', 'y', 'z'}};

which defines array of two (2) pointers to arrays of three chars.它定义了两个 (2) 指向 arrays 三个字符的指针的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM