Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Arduino - Array problem

  • 11-03-2012 11:51pm
    #1
    Registered Users, Registered Users 2 Posts: 1,115 ✭✭✭


    I'm currently doing a project that takes an input from the serial monitor in the processing compiler, converts it into a pulse sequence and transmits that to a distant receiver. It's quite easy to accomplish it with just a great dear of repetitive code but I hate doing that. The problem I'm currently having is that I want to make an array which contains numerous methods, but I cannot figure out how to do it!

    E.g. Float array[] = { method1(), method2(), ....... } ;

    I am constantly getting error messages with identifier I try. Im looking to find this out as it cuts out about 70 - 80 lines of repetitive stuff.

    For anyone who doesn't know what processing is, it's a C based language used to programme the arduino micro-controllers. :)

    Thanks!


Comments

  • Registered Users, Registered Users 2 Posts: 218 ✭✭Tillotson


    If the language is close to C, then your type signatures are probably wrong.
    float sum(float a, float b)
    {
            return a + b;
    }
    
    /* fpa is an array of pointers to (float, float) functions which return float */
    float (*fpa[])(float, float) = { sum, sum };
    


  • Registered Users, Registered Users 2 Posts: 1,042 ✭✭✭Groinshot


    I have experience with C, and Arduino, but am more than prepared to be pulled up on this..
    Can't just create an array of functions, it doesn't work. What you need to do is create an array of pointers, one pointer to each function, and store each pointer as a slot in the array. You can then call each function by dereferencing the pointer.....
    See SO if that doesn't make any sense.....
    However, I've never tried it on an arduino, so don't know if it will work or not.


  • Registered Users, Registered Users 2 Posts: 218 ✭✭Tillotson


    Groinshot wrote: »
    What you need to do is create an array of pointers, one pointer to each function, and store each pointer as a slot in the array. You can then call each function by dereferencing the pointer.

    Exactly, the above code could be written:
    float (*fpa[])(float, float) = { &sum, &sum };
    
    To invoke these functions:
    printf("%f\n", fpa[0](1.0, 2.0));
    
    or
    printf("%f\n", (*fpa[0])(1.0, 2.0));
    
    I prefer the shorter syntax but the other might be clearer. It's interchangeable either way.


  • Registered Users, Registered Users 2 Posts: 1,115 ✭✭✭magicianz


    Thanks a lot to both of ye. Only really got to look at it properly today and, for the moment, its working perfectly! :)
    float (*arrayName[]) (float,float) = { func1, func2, func3,.....};
    
    That works perfectly for me, just took a while to properly understand what you were saying. Thanks again! Saved me a lot of stress, none of the examples i've seen online are as clear


Advertisement