struct grid {
int width, height;
void *cells[];
};
typedef struct grid grid;
int main(){
enum { width = 2, height = 10 };
grid *g = malloc(sizeof(grid) + width * height * sizeof(void *));
void *(*cells)[10] = &g->cells;
}
My professor taught us about this cool way to allocate a flexible array and then assign a pointer that points to an entire array of pointers
void *(*cells)[10] = &g->cells; <-- This line
So i tried this method
char *array[10];
char *(*pointer)[2] = &array;
And it just gave me a compiler error
warning: incompatible pointer types initializing
'char *(*)[2]' with an expression of type 'char *(*)[10]'
[-Wincompatible-pointer-types]
char *(*pointer)[2] = &array;
If anyone could explain to me the functionality of a "pointer to an entire array" that would be useful. Thank you
char *(*pointer)[2]is a pointer to 2-element arrays ofchar *. Maybechar *(*pointer[2])[10]to get a 2-element array of pointers to 10-element arrays ofchar *?cellsfield aschar cells[0];, not as an incomplete type definition, which is probably the cause of compiler complainting, as the compiler cannot knowsizeof struct gridas there is an incomplete type defined there.