#define ACTION_LIST_N 3
#define MALLOC 1
#define REALLOC 2
typedef struct action action_t;
struct action {
char name; // action name
state_t precon; // precondition
state_t effect; // effect
};
main(int argc, char *argv[]) {
action_t** action_list;
make_action_list(action_list, ACTION_LIST_N, MALLOC);
action_list[0] = (action_t*)malloc(sizeof(**action_list));
printf("1"); <- DOES NOT EXECUTE
return 0;
}
void
make_action_list(action_t** action_list, int n, int request) {
if (request==MALLOC) {
action_list = (action_t**)malloc(n * sizeof(*action_list));
if_null(action_list, "Initial allocation failure");
} else if (request==REALLOC) {
action_list = (action_t**)realloc((action_t**)action_list, (n*2)*sizeof(*action_list));
if_null(action_list, "Realloc failure");
}
}
Sorry for the long post but I'm really stuck and not sure how to fix this.
I made an array of pointers to structs (action_list) and allocated memory to action_list with make_action_list(). However, when I tried calling malloc on the first pointer in the array, the program exits without any warning messages and "1" never gets printed. What am I doing wrong? Thanks in advance.
I was following the tutorial on dynamic arrays here: C: pointer to array of pointers to structures (allocation/deallocation issues)