C error de compilación: "Es posible que el objeto de tamaño variable no se inicialice"


¿Por qué recibo el error "Objeto de tamaño variable puede no ser inicializado" con el siguiente código?

int boardAux[length][length] = {{0}};
Author: Cool Guy, 2010-06-21

8 answers

Asumo que está utilizando un compilador C99 (con soporte para matrices de tamaño dinámico). El problema en su código es que en el momento en que los compiladores ven su declaración de variable no puede saber cuántos elementos hay en la matriz (también estoy asumiendo aquí, por el error del compilador que length no es una constante de tiempo de compilación).

Debe inicializar manualmente ese array:

int boardAux[length][length];
memset( boardAux, 0, length*length*sizeof(int) );
 76
Author: David Rodríguez - dribeas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-06-21 08:33:55

Recibe este error porque en el lenguaje C no se le permite usar inicializadores con matrices de longitud variable. El mensaje de error que está recibiendo básicamente lo dice todo.

6.7.8 Inicialización

...

3 El tipo de entidad que se inicializará será una matriz de tamaño desconocido o un objeto tipo que no es una longitud variable tipo array.

 20
Author: AnT,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-06-21 08:09:30

Esto da error:

int len;
scanf("%d",&len);
char str[len]="";

Esto también da error:

int len=5;
char str[len]="";

Pero esto funciona bien:

int len=5;
char str[len]; //so the problem lies with assignment not declaration

Debe poner el valor de la siguiente manera:

str[0]='a';
str[1]='b'; //like that; and not like str="ab";
 9
Author: Amitesh Ranjan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-08 09:21:38

Después de declarar el array

int boardAux[length][length];

La forma más sencilla de asignar los valores iniciales como cero es usar for loop, incluso si puede ser un poco largo

int i, j;
for (i = 0; i<length; i++)
{
    for (j = 0; j<length; j++)
        boardAux[i][j] = 0;
}
 1
Author: Krishna Shrestha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-27 12:35:57

Simplemente declare la longitud como un cons, si no lo es, entonces debería asignar memoria dinámicamente

 0
Author: Azizou,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-02 20:26:21

Para C++ separe la declaración y la inicialización de esta manera..

int a[n][m] ;
a[n][m]= {0};
 0
Author: Munawir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-25 06:34:58

Otra forma de C++ solamente:

const int n = 5;
const int m = 4;
int a[n][m] = {0};
 -1
Author: Dennis V.R.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-19 18:17:36

No puedes hacerlo. El compilador de C no puede hacer algo tan complejo en la pila.

Debe usar asignación dinámica y de montón.

Lo que realmente necesitas hacer:

  • calcule el tamaño(nmsizeof (element)) de la memoria que necesita
  • llame a malloc (size) para asignar la memoria
  • crear un accessor: int* access (ptr, x, y, rowSize) { return ptr + y*rowSize + x;}

Use *access(boardAux, x, y, size) = 42 para interactuar con la matriz.

 -5
Author: Pavel Radzivilovsky,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-06-21 07:55:41