Seem a little overcomplicated.
No need to store a pointer to the next state when you're already storing the index.
Your StateCreateInit functions can be combined:
I couldn't abide your gargantuan variable names so I shortened them.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
typedef enum State {
State0,
State1,
State2,
NumStates
} State;
typedef struct StateData {
State state;
int numNext;
State *nextStates;
bool timeout;
} StateData;
void StateCreateInit(StateData **s, State st, int numnext, ...);
int main() {
int resp;
StateData **states;
StateData *curr;
states = malloc(NumStates * sizeof(*states));
for(int i = 0; i < NumStates; i++)
states[i] = malloc(sizeof(*states[i]));
StateCreateInit(states, State0, 2, State1, State2);
StateCreateInit(states, State1, 1, State2);
StateCreateInit(states, State2, 1, State0);
curr = states[State0];
while (true) {
printf("Current State is %d. Next states: ", curr->state);
for(int i = 0; i < curr->numNext; i++)
printf("%d ", curr->nextStates[i]);
printf("\nNext state: ");
scanf("%d", &resp);
if (resp == -1) break;
for(int i = 0; i < curr->numNext; i++)
if (curr->nextStates[i] == resp) {
curr = states[curr->nextStates[i]];
break;
}
}
return 0;
}
void StateCreateInit(StateData **s, State st, int numnext, ...) {
va_list va;
va_start(va, numnext);
s[st]->state = st;
s[st]->numNext = numnext;
s[st]->nextStates = malloc(s[st]->numNext * sizeof(*s[st]->nextStates));
for (int i = 0; i < numnext; i++)
s[st]->nextStates[i] = va_arg(va, int);
s[st]->timeout = false;
va_end(va);
}
What is the timeout flag for?
How is this to be used in your actual program?