Puede hacer que funcione en gcc haciendo que la estructura sea static
o global, pero resulta que inicializar los miembros del conjunto flexible no es conforme y por lo tanto es probable que no funcione excepto con gcc. Aquí hay una manera de hacerlo que sólo utiliza funciones C99 conformes ...
#include <stdlib.h>
#include <stdarg.h>
typedef struct Grid {
int rows;
int cols;
int grid[];
} *Grid;
Grid newGrid(int, int, ...);
Grid newGrid(int rows, int cols, ...)
{
Grid g;
va_list ap;
int i, n = rows * cols;
if((g = malloc(sizeof(struct Grid) + rows * cols * sizeof(int))) == NULL)
return NULL;
g->rows = rows;
g->cols = cols;
va_start(ap, cols);
for(i = 0; i < n; ++i)
g->grid[i] = va_arg(ap, int);
va_end(ap);
return g;
}
.
.
.
Grid g1, g2, g3;
g1 = newGrid(1, 1, 123);
g2 = newGrid(2, 3, 1, 1, 1,
2, 2, 2);
g3 = newGrid(4, 5, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20);
cómo lo haces? – Macarse
He editado la pregunta anterior para ilustrar ... –
¿Qué compilador, en qué plataforma? –