0

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?

2
  • Because it is possible to initialize some (or all) elements of an array when the array is defined. Commented Aug 14, 2014 at 7:48
  • I really hope someone has the relevant historical information, but I suspect you're not going to get a better answer than speculation or "just 'cause". Commented Aug 14, 2014 at 7:48

6 Answers 6

5

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.

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

Comments

2

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

1 Comment

C has multidimensional arrays. sizeof(foo) == (sizeof(int) * 2 * MAX), there are no extra pointers.
1

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.

Comments

0

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);

Comments

0

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)

Comments

0

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]);
}

Comments

Your Answer

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