-2

In my arduino sketch I need an array of function pointers with the signature void foo(). I'm using the ino command line utillity on linux (which uses avr-g++ under the hood).

However I'm getting a very strange error while defining the array.

error:

error: invalid conversion from ‘void (* (*)())()’ to ‘void (*)()’

in this piece of code

void (*mode_setup[])(void) = {
    &show_time_setup,
    &set_time_setup,    
    &set_alarm_setup,
    &set_date_setup // <-- generates 3 identical error on this line
};

I don't understand what I'm doing wrong, since... int foo[] = { 1, 2, 3 }; ..is perfectly valid, and void (*foo)(void) is the syntax for function pointer.

what am I missing?

edit: NEVERMIND IM STUPID the functions were not void foo(), but fptr foo() [fptr=function pointer typedef] sincere appologies for wasting peoples time

4

1 回答 1

3

我可以使用此代码重现您的错误消息

void (*foo())() {}
void (*arr[])(void) = { &foo };

所以看起来函数的签名不是你想象的那样。保存指针的数组foo需要像这样声明:

void (*(*arr[])())(void) = {
    &foo
};

如果我是你,我会考虑 typedefs ......

于 2013-10-14T21:33:11.703 回答