1

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...... Commented Nov 5, 2018 at 13:17

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

@dbush I took the liberty to edit in a clarification, I hope you don't mind.
@Lundin No problem. Thanks.
Is there any way to initialize abc without knowing the size of the subarrays?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.