Why is char initialization difference? FROM

Why are these two ways to initialize an array different from each other?

The first initialization gives me a compiler warning:

while the second one works fine.

char *c_array_1[] = { {'a','b','c','d','e'}, {'f','g','h','i','j'} };

char *c_array_2[] = {"abcde","fghij"};
+3
source share
1 answer

So, in C, string literals (for example:) "abcde"automatically get the storage allocated for them against the background of the compiler.

So when you do

char *c_array_2[] = {"abcde","fghij"};

The compiler may to some extent change this to:

char *c_array_2[] = {Some_Pointer, Some_Other_Pointer};

However, for another example:

char *c_array_1[] = { {'a','b','c','d','e'}, {'f','g','h','i','j'} };

The compiler will try to initialize. This will convert this line of code to the following (and probably display a few warnings):

char *c_array_1[] = {'a', 'f'};

, , , ('a', , . , : C

+4

Source: https://habr.com/ru/post/1526008/


All Articles