array of function pointers --- syntax error!?

D

damian birchler

What's wrong about this:

22: static void (*)(void) instruction_table[] = {
jnz,
halt,
mv,
add,
mul,
mv_reg,
add_reg,
mul_reg,
pop,
push,
};

The compiler doesn't complain about wrong types of the functions, it says:

interpreter.c:22: syntax error before `)'
interpreter.c: In function `execute_instruction':
interpreter.c:66: `instruction_table' undeclared (first use in this function)
interpreter.c:66: (Each undeclared identifier is reported only once
interpreter.c:66: for each function it appears in.)

Thanks a lot
damian
 
J

Joona I Palaste

damian birchler said:
What's wrong about this:
22: static void (*)(void) instruction_table[] = {
jnz,
halt,
mv,
add,
mul,
mv_reg,
add_reg,
mul_reg,
pop,
push,
};
The compiler doesn't complain about wrong types of the functions, it says:
interpreter.c:22: syntax error before `)'
interpreter.c: In function `execute_instruction':
interpreter.c:66: `instruction_table' undeclared (first use in this function)
interpreter.c:66: (Each undeclared identifier is reported only once
interpreter.c:66: for each function it appears in.)

The error is most likely in the function execute_instruction, not in the
above declaration. Please post the code to execute_instruction.
 
K

Kenneth Brody

damian said:
What's wrong about this:

22: static void (*)(void) instruction_table[] = { [...]

The compiler doesn't complain about wrong types of the functions, it says:
[...]

"instruction_table" is the array of pointers. Therefore, you need:

static void (*instruction_table[])(void) = ...

Or, as I prefer to do:

typedef void (*VoidFunc)(void);
static VoidFunc instruction_table[] = ...

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <[email protected]>
 
J

jacob navia

damian said:
What's wrong about this:

22: static void (*)(void) instruction_table[] = {
jnz,
halt,
mv,
add,
mul,
mv_reg,
add_reg,
mul_reg,
pop,
push,
};

The compiler doesn't complain about wrong types of the functions, it says:

interpreter.c:22: syntax error before `)'
interpreter.c: In function `execute_instruction':
interpreter.c:66: `instruction_table' undeclared (first use in this function)
interpreter.c:66: (Each undeclared identifier is reported only once
interpreter.c:66: for each function it appears in.)

Thanks a lot
damian

Here is something that works

void jnz(void);void halt(void);void mv(void);void add(void);
void mul(void);void mv_reg(void);void add_reg(void); void mul_reg(void);
void pop(void);void push(void);

typedef void (*voidfn)(void) ;
static voidfn instruction_table[] = {
jnz,
halt,
mv,
add,
mul,
mv_reg,
add_reg,
mul_reg,
pop,
push,
};

C can be horrible, and arrays of function pointers belong to these
things that are unnecesarily difficult in the language.
 
?

=?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=

What's wrong about this:

22: static void (*)(void) instruction_table[] = {
jnz,
halt,
mv,
add,
mul,
mv_reg,
add_reg,
mul_reg,
pop,
push,
};

The compiler doesn't complain about wrong types of the functions, it says:

interpreter.c:22: syntax error before `)'
interpreter.c: In function `execute_instruction':
interpreter.c:66: `instruction_table' undeclared (first use in this function)
interpreter.c:66: (Each undeclared identifier is reported only once
interpreter.c:66: for each function it appears in.)

Thanks a lot
Did you mean
static void *(*instruction_table[])(void) = { ... }
?
 
D

Dave Vandervies

What's wrong about this:

22: static void (*)(void) instruction_table[] = {

The compiler doesn't complain about wrong types of the functions, it says:

interpreter.c:22: syntax error before `)'

You have a syntax error.

Quoth cdecl:
cdecl> declare instruction_table as array of pointer to function (void) returning void
void (*instruction_table[])(void )

So I think you might have wanted to say:
static void (*instruction_table[])(void) = {

'Tmight also be worth using a typedef to make things a bit simpler:

typedef void (*instruction_func_ptr)(void);
static instruction_func_ptr instruction_table[] = {/*stuff*/};

interpreter.c: In function `execute_instruction':
interpreter.c:66: `instruction_table' undeclared (first use in this function)
interpreter.c:66: (Each undeclared identifier is reported only once
interpreter.c:66: for each function it appears in.)

The compiler attempted to recover from the syntax error so it could warn
you about other things, but this particular error is bogus, because it
depends on the part that the compiler skipped doing the error recovery
in the declaration.


dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top