[Top][All Lists]

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

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

From: Oleksandr Redchuk
Subject: [avr-gcc-list] Re: calling function pointers via pointers ?
Date: Thu, 29 Sep 2005 08:01:28 +0300 (EDT)

Hi, Vincent Trouilliez!
28-Sep-05 11:53 you wrote:

VT> Problem: my function pointers are in an array, within a structure, which
VT> itself is accessed via pointers... oh dear.

I suggest using intermediate typedefs

#include <inttypes.h>

#include <avr/io.h>
#include <avr/pgmspace.h>

typedef int (*menuf)();

struct menu {
        int i;  // placeholder, just for demonstration
        menuf fp[];

int fn1();
int fn2();
int fn3();

typedef struct menu prog_menu_t PROGMEM;

const prog_menu_t mfoo = {

int mcall(const prog_menu_t *pmenu, uint8_t index) {
        menuf f = (menuf)pgm_read_word( & pmenu->fp[index] );
        return f();

avr-gcc -O2 -mmcu=atmega8 -S

        .section        .progmem.data,"a",@progbits
        .type   mfoo, @object
        .size   mfoo, 2
        .word   1
        .word   pm(fn1)
        .word   pm(fn2)
        .word   pm(fn3)
.global mcall
        .type   mcall, @function
        mov r30,r22
        clr r31
        add r30,r30
        adc r31,r31
        add r30,r24
        adc r31,r25
        adiw r30,2
        lpm r24, Z+
        lpm r25, Z
        movw r30,r24
        icall         ; hmm... why not single IJUMP ?
        ret           ; stack frame need not be handled

Of course, you can use

      return ((int (*)())pgm_read_word( & pmenu->fp[index] ))();

and generated code will be exactly the same.
But if you not so familiar with C type declarations and pointers...
typedefs will help you.

/* Oleksandr Redchuk, Brovary, Ukraine */
/* real '\x40' real '\x2E' kiev '\x2E' ua     */

reply via email to

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