Vincent Trouilliez schrieb:
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
{
...
}
void main (void)
{
int f_call;
> f_call = fptr; //load the function via the pointer
Just call it:
fptr(); //call function indirect
In your code you assign the fpointer fptr to the int f_call.
If you use this prequently for the same prototype/interface it may be
helpful to typdef a fpointer type like this:
typedef int (*fpointer_t) (int, char);
int myfunc (int i, char c)
{
return i+c;
}
int dispatch (fpointer_t func, int i, char c)
{
return func (i, c);
}
int foo (void)
{
dispatch (myfunc, 42, '?');
}
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"
see above
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.
You just are casting a pointer to an int.
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:
Here, you have to do integer arithmetic, so cast the pointer to an int
and conpute on (unsigned) integer level.
Regards,
Vince, pulling hair...
Cheers,
Georg-Johann
_______________________________________________
AVR-GCC-list mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list