What do array initialisers return?

arrays, c, pointers

Solution

Initializers do not return anything per se. They give the compiler directions as to what to put into the item being declared - in this case, they tell the compiler what to put into elements of an array.

That is why you cannot assign an initializer to a pointer: an array initializer needs to be paired with an array to make sense to the compiler.

A pointer can be initialized with a pointer expression. That is why the initialization in your

char *char_ptr_2 = char_array;

declaration works: the compiler converts `char_array` to a pointer, and initializes `char_ptr_2` with it.

Problem

What do array initialisers such as `{ 'a', 'b', 'c' }` return? My understanding is that using an initialiser allocates contiguous memory blocks and return the address to the first block. The following code doesn't work: ``` char *char_ptr_1 = { 'a', 'b', 'c', '\0' }; ``` On the other hand, this is seems to work fine: ``` char char_array[] = { 'a', 'b', 'c', '\0' }; char *char_ptr_2 = char_array; ``` `char_array` stores the address to the first memory block which explains why I am able to assign the value of `char_array` to `chat_ptr_2`. Does C convert the value returned by the initialiser to something which can be stored in a pointer? I did look online and and found a couple of answers which talked about the difference between arrays and pointers but they didn't help me.

Original source