[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[avr-gcc-list] Syntax: Function pointer hell... newbee lost ! ;-)
From: |
Vincent Trouilliez |
Subject: |
[avr-gcc-list] Syntax: Function pointer hell... newbee lost ! ;-) |
Date: |
Mon, 19 Jan 2009 22:55:25 +0100 |
Hi list,
I need a little help from the pros ;-)
I Googled for C tutorials on the net, found a few. They aren't all quite
clear about how to use function pointers, and where they are clear, the
syntax they suggest doesn't appear to work on avr-gcc :-(
I need to call a void/void function through a pointer. Here is my best
attempt based on my readings.. doesn't work, see below
main.c
---------------------------------------------------------------
.....
.....
void function(void); //prototype
void (*fptr)(); //declare the function pointer
void (*fptr) (void) = &function; //set the pointer
void function(void) //function to be started via the pointer
{
lcd_clear();
lcd_print_P( PSTR("Place Holder Func.") );
lcd_gotoxy(0,2);
lcd_print_P( PSTR("Pointer = 0x") );
//print out address held in the pointer
lcd_put_byte( (uint8_t)(fptr >> 8) );
lcd_put_byte( (uint8_t)(fptr & 0x00FF) );
}
void main (void)
{
int f_call;
f_call = fptr; //load the function via the pointer
....
}
------------------------------------------------
Sadly it doesn't work.
GCC does issue a warning (might be the problem ?) about the
function call itself (f_call = fptr), saying:
"warning: assignment makes integer from pointer without a cast"
Problem is that tutorials say to use a variable of the same type
than that returned by the function (does make sense ;-) in the
assignement statement that executes the function but... since my
function actually returns nothing, I would have to use a variable of
type "void", to be consistent... which doesn't make sense does it..
So as a workaround, I tried casting the pointer to int, as suggested by
the compiler, like this
f_call = (int) fptr;
... but although it appears to make the compiler happy, it doesn't work
either.
I also have another problem related to the pointer: to try and
start figuring things out, I meant to display the content/address
stored in the pointer, onto the LCD, to see if it matches the address
of the function as seen in the map file.
Since I have an ATmega32, I assume the pointer must be 16 bits, so as
you can see in the body of the function, I tried to display the high
byte first then low byte of the pointer, but I guess there must be some
trick there too, because gcc complains again:
*****************
error: invalid operands to binary >>
error: invalid operands to binary &
*****************
Since one can do arythmetic operations to pointers, like add or
substract, I assumed/hoped one could do "any" kind of operation on it,
like you would on a normal data. Maybe not then ?
Thanks in advance for your time...
Regards,
Vince, pulling hair...
- [avr-gcc-list] Syntax: Function pointer hell... newbee lost ! ;-),
Vincent Trouilliez <=