array of function pointers --- syntax error!?

Discussion in 'C Programming' started by damian birchler, Oct 20, 2004.

  1. 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
    damian birchler, Oct 20, 2004
    #1
    1. Advertising

  2. damian birchler <> 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 () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "There's no business like slow business."
    - Tailgunner
    Joona I Palaste, Oct 20, 2004
    #2
    1. Advertising

  3. 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: <>
    Kenneth Brody, Oct 20, 2004
    #3
  4. damian birchler

    jacob navia Guest

    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.
    jacob navia, Oct 20, 2004
    #4
  5. 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) = { ... }
    ?
    =?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=, Oct 20, 2004
    #5
  6. In article <>,
    damian birchler <> 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
    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
    Dave Vandervies, Oct 20, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron Walker
    Replies:
    4
    Views:
    699
    Aaron Walker
    Oct 1, 2005
  2. Sean
    Replies:
    2
    Views:
    625
    loufoque
    Sep 24, 2006
  3. Piotrek

    pointers and array of pointers

    Piotrek, Apr 2, 2007, in forum: C Programming
    Replies:
    8
    Views:
    314
    Chris Torek
    Apr 6, 2007
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    642
  5. Mark Richards
    Replies:
    3
    Views:
    296
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page