error: variable-sized object may not be initialized

1

The elements of chair points fulfill one of these conditions:

  • Within the row are minimal and within the column are maximum.
  • Inside the row they are maximum and inside the column they are minimum
  •  #include <stdio.h>
     #define N 10
    
    typedef struct
    {
        int fils, cols;  
        unsigned int mat[N][N];
    }tmatriz;   
    
    
    int main()
    {
        tmatriz m1={4,3,{{4,4,1},{4,3,0},{3,5,0}, {4,2,0}}};
    
        int i, j;
        int maxFils [m1.fils] = {-999, -999, -999, -999};
        int minCols [m1.cols] = {999, 999, 999};    
        printf("Datos de la matriz m1:\n");
        for (i=0; i<m1.fils; i++)
        {
            for (j=0; j<m1.cols; j++)
                printf("%3u ", m1.mat[i][j]);
            printf("\n");
        }
        for (i=0; i<m1.fils; i++){
            for (j=0; j<m1.cols; j++){
                if (m1.mat [i] [j] > maxFils [i]){
                    maxFils [i] = m1.mat [i] [j];
                }
                if (m1.mat [i] [j] < minCols [j]){
                    minCols [j] = m1.mat [i] [j];
                }
            }
        }
        for (i=0; i<m1.fils; i++){
            for (j=0; j<m1.cols; j++){
                if (maxFils [i] == minCols [j]){
                    printf ("Punto de silla en (%d, %d) y su valor es %d\n", i, j, m1.mat [i] [j]);
                }
            }
        }
    }
    

    I do not understand why this algorithm does not work for me

        
    asked by Jordi García 25.04.2017 в 15:08
    source

    1 answer

    1

    I guess you're in C99, or that you use a compiler with variable length arrays support. The compiler tells you very clearly:

      

    variable-sized object may not be initialized

    You are using a array of variable length : the size of array is not known at compile time >, but in execution time .

    At the moment of generating the code to initialize said array maxFils , the compiler does not know its size . So, does not know how many data to expect, nor how many bytes copy . The compiler does not know how to initialize it, precisely because it does not know its size.

    You have 2 options:

    Use fixed length arrays :

    int maxFils[N] = { -999, -999, ... };
    

    Initializes directly the elements of the array:

    int maxFils[m1.fils];
    int idx;
    
    for( idx = 0; idx < m1.fils; ++idx )
      maxFils[idx] = -999;
    

    The 1st option is the most portable among compilers.

        
    answered by 25.04.2017 / 16:09
    source