4

I can initialize a one dimensional array in c with or without initializing its size:

int x[] = {1,2,3,4,5};
int y[5] = {1,2,3,4,5};

But, when I try to do the same for a two dimensional array such as

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

I get an error: array type has incomplete element type. The same error occurs if I declare and initialize the array on different lines. However, I can initialize it while stating the size:

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

There is no error with this one. My question is, is it possible to initialize a multi dimensional array without first initializing its size? I ask this because for an eventual project, I need to be able to declare arrays and initialize them later, and their size will not be known when compiling.

9
  • 2
    If it has dynamic size, how will you provide the initializer list in the code? Commented Sep 23, 2017 at 1:13
  • 2
    What do you mean by "declare and initialize the array on different lines"? The only way to initialize an array is in the declaration. Commented Sep 23, 2017 at 1:14
  • @Barmar my bad I think I meant to say define. As in have int x[][]; and then somewhere else type x[][] =... Commented Sep 23, 2017 at 1:46
  • 1
    If you don't know the size and initial values until run time, then you obviously can't initialize it with literals like that. Use either a VLA or dynamic allocation with malloc(). Commented Sep 23, 2017 at 23:41
  • 1
    @Comrade_Comski Regardless of how you declare it, you can never assign to an array, you can only assign to individual elements. Commented Sep 23, 2017 at 23:44

3 Answers 3

5

is it possible to initialize a multi dimensional array without first initializing its size?

No, not in the way you are proposing or anything similar to that.

I need to be able to declare arrays and initialize them later, and they will have dynamic sizes.

If the size is unknown at compile time, you should allocate the array using a = malloc(x * y * sizeof(value_t)). Then index into it like a[i + j*y].

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

3 Comments

Isn't variable-length arrays also a possibility?
Note that you should check the x*y*sizeof(value_t) multiplications for integer overflows.
A VLA does not need dynamic allocation. If you want to use it, you can still use a VLA. Using a 1D array and manually calculating the index is not a 2D array, error prone and outdated since 18 years.
1
Is it possible to initialize a multi dimensional array without first initializing its size?

=> No, it is not possible.

The thing possible is that you take the size of the array and then allocate memory using calloc. I'm asking you to use calloc because this way all the array elements will be initially initialized as 0.

Now, use of calloc will be applied as:

Assuming you want 2D array, so you take variable row and column as input from the user. Then use,

int *x;
x = calloc( ( row * column), sizeof(datatype) );

In this case the datatype would be int.

In short code would appear as :

int row, column, *x;

    /* TAKING INPUT FROM THE USER */
printf("\n\t Enter the number of rows : ");
scanf("%d", &row);
printf("\n\t Enter the number of columns : ");
scanf("%d", &column);

    /* DYNAMICALLY ALLOCATING MEMORY TO x USING calloc */
x = calloc( (row * column), sizeof(int));

I hope this code solves your problem.

I have one more thing to share with you.

In your this line of code :

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

This initialization is just missing one thing and that thing is mention of column size and otherwise code is correct.

So, Correction to your code :

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

This would work fine.

Keep on trying! These type of things can only be known while practicing.

Happy Coding!

Comments

-4

Yes, but you're missing a comma:

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

All array elements (even inner arrays) need to be comma-separated

5 Comments

Wouldn't that cause a syntax error, not "array has incomplete type"?
All I know is that he's got a syntax error... stackoverflow.com/questions/15520880/…
He also has that syntax error in the version that he says works. I think he just copied it wrong into the question.
His question was how to do it without putting [2][3] into the size.
Ahh I see that now, my bad.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.