[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] calling function pointers via pointers ?

From: Dave Hansen
Subject: RE: [avr-gcc-list] calling function pointers via pointers ?
Date: Wed, 28 Sep 2005 09:16:18 -0400

From: Vincent Trouilliez <address@hidden>
Problem: my function pointers are in an array, within a structure, which
itself is accessed via pointers... oh dear.

//data type for a menu
struct menu {
        int (*fp)()[];  //table to store all the pointers

Err... Read it from the inside-out: fp is a pointer to a function that returns an array of pointers to int. I don't think that works, let alone is what you want. And you need to specify the size, and it's a good idea to specify the formal parameters needed by the function. I think you mean something like

  int (*fp[3])(void);

fp is an array of 3 pointers to function taking void and returning int.


//definition for a menu
const struct menu __ATTR_PROGMEM__ menu_foo = {
        {fn1, fn2, ... }

Note: menu_foo is PROGMEM.

//run the specified menu using the structure passed as argument
void menu_run(const struct menu * p)

Do you have to make p point to PROGMEM?  I'm not sure, but I do know that...
        int r;
        r = (p->fp[0])();    //call the first function

...this won't work if you call menu_run with &menu_foo as the parameter. You need to read the pointer out of PROGMEM first, then call using that:

     int (*fp)(void);
     fp = pgm_read_word(&p->fp[0]);

Warning: untested code.  You might need a cast in there.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]