variable-sized object may not be initialized?

0
  

Good Night. I'm having a problem compiling a file. The errors are indicated below:

    example.c: In function ‘main’:
example.c:125:5: error: variable-sized object may not be initialized
     AGENT *grid[WORLD_X][WORLD_Y] = { NULL };
     ^
example.c:125:5: warning: excess elements in array initializer
example.c:125:5: note: (near initialization for ‘grid’)
example.c: In function ‘example_get_ag_info’:
example.c:355:15: error: ‘WORLD_X’ undeclared (first use in this function)
     if ((x >= WORLD_X) || (y >= WORLD_Y)) {
               ^
example.c:355:15: note: each undeclared identifier is reported only once for each function it appears in
example.c:355:33: error: ‘WORLD_Y’ undeclared (first use in this function)
     if ((x >= WORLD_X) || (y >= WORLD_Y)) {
                                 ^
example.c:351:13: warning: variable ‘grid’ set but not used [-Wunused-but-set-variable]
     AGENT **grid = (AGENT **) w;
             ^'insira o código aqui'
  

Errors indicate that the variable I am using can not be used inside the array pointer, but I need its value (being variable up to the beginning of the program and after indicating its value, always constant) that is possible to use.   The code where this happens is as follows:

int main(int argc, char *argv[]) {

    int WORLD_X=0;
    int WORLD_Y=0;
    int NZOMBIES=0;
    int NHUMANS=0;
    int NZOMBIES_PLAY=0;
    int NHUMANS_PLAY=0;
    int TURNS=0;
    int contar = 0; 

    /* Verificar se numero de argumentos foi o correto */
    /* Parametros e variaveis*/
    if(argc != 15){
        fprintf(stderr,
                "Exemplo de uso:\n\t%s "
                "-x 20 "
                "-y 20 "
                "-z 10 "
                "-h 30 "
                "-Z 1 "
                "-H 2 "
                "-t 1000\n",
                argv[0]);
            exit(-1);
    }
    for(int i=1;i<=argc;i=i+2){
        if(strcmp(argv[i], "-x") == 0){
            WORLD_X=atoi(argv[i+1]);
        }
        if(strcmp(argv[i], "-y") == 0){
            WORLD_Y=atoi(argv[i+1]);
        }
        if(strcmp(argv[i], "-z") == 0){
            NZOMBIES=atoi(argv[i+1]);
        }
        if(strcmp(argv[i], "-h") == 0){
            NHUMANS=atoi(argv[i+1]);
        }
        if(strcmp(argv[i], "-Z") == 0){
            NZOMBIES_PLAY=atoi(argv[i+1]);
        }
        if(strcmp(argv[i], "-H") == 0){
            NHUMANS_PLAY=atoi(argv[i+1]);
        }
        if(strcmp(argv[i], "-t") == 0){
            TURNS=atoi(argv[i+1]);
        }
    }

    /* An instance of a SHOWWORLD world display. */
    SHOWWORLD *sw = NULL;

    /* A by-dimensional array of agent pointers, representing agents in a
       grid.
       All elements in this grid are initialized to NULL, i.e., they don't
       initially point to anything. */
    AGENT *grid[WORLD_X][WORLD_Y] = { NULL };
    
asked by anonymous 18.02.2018 / 01:05

0 answers