简体   繁体   中英

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.
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.

  1. braces around scalar initializer
  2. initialization makes pointer from integer without a cast
  3. excess elements in scalar initializer

I got that *numbers[] kind of things should have pointer values in it.
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. Please explain:)

the second initialization is invalid as {'a', 'b', 'c'} is not the array which can dacal to pointer

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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