Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > array of function pointers --- syntax error!?

Reply
Thread Tools

array of function pointers --- syntax error!?

 
 
damian birchler
Guest
Posts: n/a
 
      10-20-2004
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
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      10-20-2004
damian birchler <(E-Mail Removed)> scribbled the following:
> 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.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"There's no business like slow business."
- Tailgunner
 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      10-20-2004
damian birchler wrote:
>
> 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: <(E-Mail Removed)>

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      10-20-2004
damian birchler wrote:
> 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.
 
Reply With Quote
 
=?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=
Guest
Posts: n/a
 
      10-20-2004
On Wed, 20 Oct 2004 11:45:43 -0700, damian birchler wrote:

> 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) = { ... }
?
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      10-20-2004
In article <(E-Mail Removed) >,
damian birchler <(E-Mail Removed)> wrote:
>What's wrong about this:
>
>22: static void (*)(void) instruction_table[] = {


<snip>

>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

--
Dave Vandervies http://www.velocityreviews.com/forums/(E-Mail Removed)
But they're not strings. Not being strings doesn't stop them being useful.
After all, ints aren't strings, yet they are very useful indeed!
--Richard Heathfield in comp.lang.c
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
pointers and array of pointers Piotrek C Programming 8 04-06-2007 08:02 AM
Pointers, linked list, array of pointers Sean C++ 2 09-24-2006 01:35 PM
void pointers & void function pointers Peter Goddard C Programming 3 05-16-2005 09:44 PM
Template specialization of pointers with function pointers Phil C++ 1 09-16-2003 02:17 AM



Advertisments