-1

I am reading two values from a text file and saving them into an array, lets call it array1. Now, is it possible to initialize a 2D array using the elements of arrays1 ? (i.e. char array2 [array[0]][array[1]]; )

I already tried this and the program started acting weird. I also tried moving the values of the elements in an integer and then use the integer as the array size. This method did not work either.

Any suggestions of how I can implement this please?

dritech
  • 87
  • 3
  • 10

1 Answers1

0

A 2D array can be initialized using the array initialization syntax only if second the dimension of the array is known at compile time.

Example:

// Create a 1 x 2 array.
char array2[][2] = {{array[0], array[1]}};

// Create a 2 x 2 array.
// Only array2[1][0] and array2[1][0] are initialized
// from array. Rest are zero initialized.
char array2[][2] = {{array[0]}, {array[1]}};

// Create a 3 x 2 array.
// Only array2[0][1] and array2[0][1] are initialized
// from array. Rest are zero initialized.
char array2[3][2] = {{array[0], array[1]}};
R Sahu
  • 204,454
  • 14
  • 159
  • 270