I am trying to create a structure on the heap - so that it can be used in other functions, but I can't seem to get past the first hurdle of creating the structure itself in a way that I can pass it along with a pointer. I know that the field arrays are properly initialized 5 by 5, and I can get the pointer (*f) to the fieldz struct, but when I try to pass it back to main, nothing comes along. I need to keep field_t * f = makeField(w,h); as is, I feel like I am missing something obvious.
Thanks!
struct _field_t {
int ** field;
int width;
int height;
};
typedef struct _field_t field_t;
field_t * makeField(int w, int h){
field_t fieldz;
fieldz.width = w;
fieldz.height = h;
int ** fieldz = malloc(h * sizeof(* fieldz));
for(int y = 0; y < h; y++){
int * row = malloc(w * sizeof(* row));
fieldz[y] = row;
for(int x = 0; x < w; x++){
row[x] = -1;
}
}
fieldz.field = field;
field_t * f = &fieldz;
return f;
}
int main() {
int h = 5;
int w = 5;
field_t * f = makeField(w, h);
return 0;
}