Skip to main content
added 632 characters in body
Source Link
SHuss
  • 41
  • 2
  • 7

I am trying to initialize a multidimensional array in the following way but I am not sure if it is correct. I am re-initializing large tables implemented using multidimensional arrays and I am not sure how to do it. I need to initialize the entire row at a time and cannot initialize the elments individually.

int array[3][3];
int ind = 0;
array[ind++] = {1,2,3};
array[ind++] = {4,5,6};
array[ind++] = {7,8,9};

Ok so i cannot do something like array[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; because what I actually want to do is something like this

int array[][3];
int ind = 0;
array[ind++] = {1,2,3};
if(contion1)
   array[ind++] = {4,5,6};
else 
   array[ind++] = {0,0,0};
array[ind++] = {7,8,9};

I hope this makes it more clear. This is not ideal, I know but I was handed over the code with #ifs something like this

int array[][3] = {
     {1,2,3},
     #if contion1
        {4,5,6},
     #else 
        {0,0,0},
    {7,8,9}};

and was asked to get rid of the #ifs.

I am trying to initialize a multidimensional array in the following way but I am not sure if it is correct. I am re-initializing large tables implemented using multidimensional arrays and I am not sure how to do it. I need to initialize the entire row at a time and cannot initialize the elments individually.

int array[3][3];
int ind = 0;
array[ind++] = {1,2,3};
array[ind++] = {4,5,6};
array[ind++] = {7,8,9};

I am trying to initialize a multidimensional array in the following way but I am not sure if it is correct. I am re-initializing large tables implemented using multidimensional arrays and I am not sure how to do it. I need to initialize the entire row at a time and cannot initialize the elments individually.

int array[3][3];
int ind = 0;
array[ind++] = {1,2,3};
array[ind++] = {4,5,6};
array[ind++] = {7,8,9};

Ok so i cannot do something like array[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; because what I actually want to do is something like this

int array[][3];
int ind = 0;
array[ind++] = {1,2,3};
if(contion1)
   array[ind++] = {4,5,6};
else 
   array[ind++] = {0,0,0};
array[ind++] = {7,8,9};

I hope this makes it more clear. This is not ideal, I know but I was handed over the code with #ifs something like this

int array[][3] = {
     {1,2,3},
     #if contion1
        {4,5,6},
     #else 
        {0,0,0},
    {7,8,9}};

and was asked to get rid of the #ifs.

Source Link
SHuss
  • 41
  • 2
  • 7

initializing multidimensional array in C

I am trying to initialize a multidimensional array in the following way but I am not sure if it is correct. I am re-initializing large tables implemented using multidimensional arrays and I am not sure how to do it. I need to initialize the entire row at a time and cannot initialize the elments individually.

int array[3][3];
int ind = 0;
array[ind++] = {1,2,3};
array[ind++] = {4,5,6};
array[ind++] = {7,8,9};