3

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.

Daniel
  • 509
  • 1
  • 4
  • 17
  • 2
    If it has dynamic size, how will you provide the initializer list in the code? – Barmar Sep 23 '17 at 01: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. – Barmar Sep 23 '17 at 01:14
  • @Barmar my bad I think I meant to say define. As in have int x[][]; and then somewhere else type x[][] =... – Daniel Sep 23 '17 at 01:46
  • @Barmar As for dynamic sizes, that was a bad way of wording it on my part. What I mean is that the size if the array will not be known when compiling. – Daniel Sep 23 '17 at 02:01
  • To extend what @barmer wrote: YOu can only initialise a variable **with** the declaration. Which makes this a definition (not the only way for a definition, but definitively one). And if the size is not know at compile time, you cannot initialise it at compile time (sounds reasonable to me). Maybe you want to do some research in your C book about declaration, definition and initialisation. – too honest for this site Sep 23 '17 at 02:36
  • Learn about VLAs, available since C99 (yet made optional in C11; still widely available). – ad absurdum Sep 23 '17 at 02:48
  • If you know the list of values to initialize with, as in your attempted `int[x][y]` initialization, then how can you not know the size? Leaving the size blank is merely a convenience in the first code line -- the compiler counts the initializers instead of making you do it. But it's still known at compile time. – Barmar Sep 23 '17 at 23:40
  • 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()`. – Barmar Sep 23 '17 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. – Barmar Sep 23 '17 at 23:44

3 Answers3

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

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    Isn't variable-length arrays also a possibility? – Barmar Sep 23 '17 at 01:15
  • Note that you should check the `x*y*sizeof(value_t)` multiplications for integer overflows. – ndim Sep 23 '17 at 01:18
  • 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. – too honest for this site Sep 23 '17 at 02:39
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!

Jorvis
  • 386
  • 5
  • 13
-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

ACVM
  • 1,497
  • 8
  • 14