storing stuff into a variable size structure
I have a follow up question to something I asked a couple of days ago. I
have a variable length structure,
typedef struct
{
int *positions1, *positions2;
char *symbols, *args;
char (*names)[100];
} agentInfo;
int initStruct(agentInfo *ai, int x)
{
ai-> positions1 = malloc(x * sizeof(int));
ai-> positions2 = malloc(x * sizeof(int));
ai-> symbols = malloc(x);
ai-> args = malloc(x);
ai-> names = malloc(100 * x);
return 0;
}
Which I call and create an instance of (mystruct). And I want to read
stuff into it each array so that I can access it using an index. I was
thinking something like,
int a, b;
char c, d;
char e[100];
char buffer[100];
while(fgets(buffer, 100, fp) != NULL){
if(sscanf(buffer, "%d %d %c %s %c", &a, &b, &c, e, &d))
mystruct -> positions1[i] = a;
mystruct -> positons2[i] = b;
mystruct -> symbols[i] = c;
strcpy((mystruct -> names[i]), e);
mystruct -> args[i] = d;
i++;
}
}
When I print a, b, c, d, e on each iteration, they appear correct. If I do,
printf("%d %d %c %s %c\n", (mystruct-> positions1[i]), (mystruct ->
positions2[i]),
(mystruct->symbols[i]), (mystruct -> names[i]), (mystruct -> args[i])
It doesn't seem to have read the values into my structure. What am I doing
wrong with my indexing?
No comments:
Post a Comment