2

code snippet:

int *c[2] = {{1,2,3}, {4,5,6}};

gives warning:

warning: incompatible integer to pointer conversion initializing 'int *' with an expression of type 'int'
      [-Wint-conversion]
    int *c[2] = {{1,2,3}, {4,5,6}};
                  ^
warning: excess elements in scalar initializer
    int *c[2] = {{1,2,3}, {4,5,6}};
                    ^

I suppose array {1,2,3} would decay to pointer so the assignment be legit?

Further more, according to the warning, why does the compiler think i'm trying to assign int to int *? instead of int array type to int *? Thanks in advance!

3
  • You are trying to initialise int c[2][3] with the given data. Commented Apr 24, 2019 at 18:04
  • Hi Weather, c is a 2-element array of pointers, you can gcc -S to check the assembly implementation compared with int cc[2][3]. Commented Apr 24, 2019 at 18:07
  • 1
    I meant: the data as provided, would be for int c[2][3] Commented Apr 24, 2019 at 18:08

2 Answers 2

3

Bracketed initializers are not arrays, and so cannot undergo decay to a pointer type. And because c is an array of int *, each initializer needs to be of that type. Nested initializers only work with actual arrays (not pointers) or structs.

What you can do however is use compound literals in the initializer which do have an array type.

int *c[2] = {(int []){1,2,3}, (int []){4,5,6}};
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks dbush, but why does compiler say the code is initializing int * with int? Does initializer have the type of int?
@mzoz Each value in the initializer list is an int, so that's what it attempts to assign to the elements of c. The extra braces don't make the initializer an array.
1

I suppose array {1,2,3} would decay to pointer so the assignment be legit?

No. {1, 2, 3} is not an array, and no decay applies to it. It is an initializer suitable for initializing an array of three or more elements of arithmetic type, but you are trying to use it to initialize a pointer. You could do something like this, instead:

static int x[] = { 1, 2, 3 };
static int y[] = { 4, 5, 6 };
int *c[] = { x, y };

Or you could use compound literals to avoid declaring variables x and y, as another answer suggests. In either of these cases, the initializer elements are arrays, and they do decay to pointers.

2 Comments

Thanks John, but why does compiler say the code is initializing int * with int? Does initializer have the type of int?
The first element of {1, 2, 3} does, and that's what the compiler is looking at. The initializer for a scalar (including a pointer) can optionally be enclosed in {}. GCC has diagnosed a small-picture item instead of recognizing and diagnosing the larger issue, and it is understandable that it does so.

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.