Reputation: 2463
I have this struct:
typedef struct {
void (*func)(instruction);
union {
double db;
char ch;
};
} instruction;
Within my source file, I have an array of these instructions. My question is, how would I loop through that array and execute each struct's function?
I thought I knew how to do this, but the fact that the instruction function has a parameter that is an instruction seems to cause problems. As such, this does not work:
int i;
for (i = 0; i <= instr_count; i++) {
(*instr[i].func) (instr[i]);
}
instr being the array of instructions.
Here is the function that populates the array. The array itself is declared at the top of the file.
void read_instructions()
{
char* str;
char *instruct;
int n;
char c;
instruction next = {0};
while (!feof(datafile)) {
// Fetch the next string
// if (push or pop), get the next argument
// create instructiwn and add to instruction array
str = get_next_string();
instruct = strtok (str, " ");
if (strncmp (instruct, "P", 1) == 0) {
char *arg = strtok (NULL, " ");
if (strncmp (str, "PUSH", 4) == 0) {
next.func = pushFunc;
}
else {
next.func = popFunc;
}
n = arg[0];
if (n > 64 || n < 71)
next.ch = n;
else {
double n;
scanf ("%lf", arg, n);
next.db = n;
}
instr[instr_count] = next;
instr_count++;
}
else {
c = instruct[0];
switch (c) {
case 'A' :
next.func = addFunc;
break;
case 'S' :
next.func = subFunc;
break;
case 'M' :
next.func = multFunc;
break;
case 'D' :
next.func = divFunc;
break;
default :
break;
}
instr[instr_count] = next;
instr_count++;
}
}
fclose (datafile);
}
As a quick explanation, this code takes a file of "intermediate code", determines the instruction, and creates an instruction struct for each that holds the proper function pointer.
Running the code gives me this runtime error:
"Unhandled exception at 0x00000000 in Interpreter.exe: 0xC0000005: Access violation." (Compiled using VS 2010).
Upvotes: 1
Views: 2140
Reputation: 126193
Depending on whether instr
is an array of instruction
or instruction *
, you want either
instr[i].func(&instr[i]);
or
instr[i]->func(instr[i]);
I suspect you want the first (array of instruction
), as what you have will compile (with warnings on most compilers) for that; you'll just get garbage for the argument in the function.
The exception you are getting suggests that this isn't your problem, however -- you probably have an instruction with a NULL func
pointer in your array.
Edit
Looks like you're making the classic while(!feof(input))
error -- whenever you see this, its almost always wrong.
The problem is that after you read the last line of the input, feof
will still return false -- it won't return true until you've tried to read PAST the end of the input an received an EOF result. So you'll get an extra iteration of the loop with an blank line after the end of your input code, which probably results in the crash you see.
what you probably want is something like while (!(str = get_next_string()))
or perhaps while (!(str = fgets(buffer, sizeof(buffer), datafile)))
Upvotes: 1
Reputation: 3370
It's hard to tell without looking at the rest of your code, but the address listed in the error (0x00000000
) implies that one of your function pointers in the array is NULL
.
Could you verify that your array is correctly initialized before you walk through it?
Upvotes: 0