0

Dimensionless initialization of a structured multi-dimensional array (matrix).

Dimensionless initialization of a single dimension array

int myarray[] = { 0, 1, 2, 3, 4 5 };

Initialization of the multidimensional array.

This is how it is normally done

int myarray[3][2] = { {0, 1}, {2, 3}, {4, 5} };

Without specifying the first dimension size, inferred

int myarray[][2] = { {0, 1}, {2, 3}, {4, 5} }; 

This does not work. I don't know why? I such a thing possible? I don't see why not. I just can't figure out how.

int myarray[][] = { {0, 1}, {2, 3}, {4, 5} }; 

Now try that on a multidimensional structured array. That is my end goal. That's why I would like to accomplish.

  struct DATA_T
    {
      int B1;
      int B2;
      int B3;
      int B4;
    };
/* 
 * Infer the dimensions of the array from assignment
 * Remove the 4 and get it to work
 */
struct DATA_T DATA[][4] =
{
        {
        { 1, 1, 1, 1 },
        { 2, 2, 2, 2 },
        { 3, 3, 3, 3 },
        { 4, 4, 4, 4 },
        },
        {
        { 5, 5, 5, 5 },
        { 6, 6, 6, 6 },
        { 7, 7, 7, 7 },
        { 8, 8, 8, 8 },
        }
 };

TIA,

  • 1
    When you define an array, the compiler needs to know array of *what* (exact type). Its size can be inferred from the initializer. In case of two dimensional array, you actually define array of arrays. So the "what" in this case is the exact type of the internal arrays, including its size. This is why `int myarray[][]= {...}` won't work. – Eugene Sh. Apr 30 '20 at 19:53
  • 1
    This is not supported because it is a nuisance for the compiler. The inner braces do not all necessarily contain the same numbers of values. So the compiler would have to find the longest one to know the dimension. Then it has to return to the beginning to arrange all the values in the proper locations. C is largely designed to avoid requiring the compiler to back up like that. – Eric Postpischil Apr 30 '20 at 19:59

0 Answers0