1
votes

I have below code :

int main()
{
    int* abc[] ={
                    [3] = (&(int[3]){1,2,3}),
                    [2] = (&(int[2]){4,5})
                };
    printf("\n abc[3][1] = %d \n",abc[3][1]);
    return 0;
}

I am trying to set up my array abc , so that specific indexes of the array point to a different array of integers.

Later, I would modify this to use macros so that array is initialized during pre-processing, hence such an approach.

Code works fine but I get a warning :

warning: initialization from incompatible pointer type

Is this because my array abc is declared to point to integer but it is actually pointing to array of integers? How can I make this warning go away?

1
FormatTry...... - gsamaras

1 Answers

3
votes

The types you're using in the compound literals are incorrect.

The elements of the array are int *, but when using &, the types of the compound literals are int (*)[3] and int (*)[2].

It should be:

int* abc[] ={
                [3] = ((int[3]){1,2,3}),
                [2] = ((int[2]){4,5})
            };

Now int[3] and int[2] both decay to int *.