In C, because of the framework I use and generate though a compiler, I am required to use global variable length array.
However, I can not know the size of its dimension until runtime (though argv for example).
For this reason, I would like to declare a global variable length array with unknown size and then define its size.
I have done it like that :
int (*a)[]; //global variable length array
int main(){
//defining it's size
a = (int(*)[2]) malloc(sizeof(int)*2*2);
for(int i=0;i<2; i++){
for(int j=0;j<2; j++){
a[i][j] = i*2 + j;
}
}
return 0;
}
However, this does not work : I get the invalid use of array with unspecified bounds error. I suspect it is because even if its size is defined, its original type does not define the size of the larger stride.
Does someone know how to solve this issue ? Using C99 (no C++) and it should be quite standard (working on gcc and icc at least).
EDIT: I may have forget something that matters. I am required to propose an array that is usable through the "static array interface", I mean by that the multiple square bracket (one per dimension).
mallocit anyway, what's the point of the VLA? Just use a pointer.int *a = 0; int a_size = 0;and then assign values appropriately. (Or an array of pointers —int **a = 0;)void*and asizevariable though and then cast thevoid*to an appropriate size VLA every time you have to use.