user1341676
user1341676

Reputation:

Initializing multidimensional arrays in C

I'm just a tad confused about initializing multidimensional arrays in C...

This works:

 int foo[2][MAX] = {
    {2,4,34,43,23,0},
    {2,4,34,43,23,0}
 };

But this does not:

int foo[2][MAX];
foo = {
    {2,4,34,43,23,0},
    {2,4,34,43,23,0}
};

How come?

Upvotes: 0

Views: 326

Answers (6)

Sathish
Sathish

Reputation: 3870

You have misunderstood the concept of 'initialization' and 'assignment`.

For example

int a = 10; // Initialization

Initialization is nothing but declaration + assignment.

But

int a; // Declaration
a = 10; // Assignment

When you do like this-

int foo[2][MAX] = {
   {2,4,34,43,23,0},
   {2,4,34,43,23,0}
};

internally it came to know 2 rows and MAX columns. It will initialize the whole array.

But-

int foo[2][MAX];
foo = {
    {2,4,34,43,23,0},
    {2,4,34,43,23,0}
};

Here foo represent the starting address of the array. Here you are trying to assign the values to 2D array. when you are trying to assign values to array it doesn't know how many rows and how many columns are there. so it is not possible and not allowed.

When you want to assign the input to array A better solution is scan it from user/ at run time-

int foo[2][MAX],i,j;
for(i=0;i<2;i++){
   for(j=0;j<max;j++)
       scanf("%d",&foo[i][j]);
}

Upvotes: 0

David Ranieri
David Ranieri

Reputation: 41017

In C99 (or in gcc as an extension) you can make use of compound literals:

int (*foo)[MAX];

foo = (int [][MAX]) {
    {2,4,34,43,23,0},
    {2,4,34,43,23,0}
};

But note that foo must be declared as a pointer to MAX int's (not as a 2D array)

Upvotes: 0

AnT stands with Russia
AnT stands with Russia

Reputation: 320361

Multidimensional or not, arrays in C are not copyable, which means that there's no way to assign anything to the entire array using core language features after the initialization is complete.

However, it is still possible to copy arrays by using memcpy function. In combination with compound literals it allows you to do what you tried to do. You just have to use a different syntax

int foo[2][MAX];

memcpy(
  foo, 
  (int[2][MAX]) {
    {2,4,34,43,23,0},
    {2,4,34,43,23,0}
  },
  sizeof foo);

Upvotes: 0

Yu Hao
Yu Hao

Reputation: 122363

As you said, this syntax is used to initialize an array, but in your second piece of code:

int foo[2][MAX];

Here, foo is uninitialized, and then

foo = {
    {2,4,34,43,23,0},
    {2,4,34,43,23,0}
};

This is assignment, not initialization.

Upvotes: 1

webNeat
webNeat

Reputation: 2828

This syntax is for initialization, but in the second case you are using it for assignment which will not work.

type varName = someValue; // this is declaration and initialization

type varName; // this is declaration
varName = someValue; // this is assignment and not initialization

That is initialization can only be done at the declaration time, else it's a normal assignment.

Upvotes: 5

Frank H.
Frank H.

Reputation: 1330

The { syntax is only valid when you're initializing and declaring an array at the same time. After declaring it, you need to use the complete syntax:

foo[0][0] = 2;

Technically speaking, C only has one-dimensional arrays. You create multidemnsional arrays by making arrays of arrays. The name of an array is converted to a pointer to its first element, and only the outer array is converted to a pointer. It's a pointer to an array of MAX ints, or int(*)[MAX].

Upvotes: 2

Related Questions