I'm new to Arduino, and I'm trying to decipher this code. There are various void functions that I am trying to call in response to receiving serial data (the Monitor outputs "Please enter valid function to call"), but I suppose I don't really know how.

typedef void (* Caller)();
Caller FuncCall[] = {a, b, c};
String func_list[] = {"a","b","c"}; //correspond to functions in FuncCall, order matters.

"a", "b", and "c" are 3 separate functions that are written out later in the code.

Am I missing something here? I can elaborate if needed.

  • \$\begingroup\$ What is "in the Serial Monitor" supposed to mean? \$\endgroup\$ – Ignacio Vazquez-Abrams Jun 30 '14 at 20:08
  • \$\begingroup\$ I am under the impression that you can call functions within the Serial Monitor (after pressing the magnifying glass there's a text entry space and a Send button) \$\endgroup\$ – M.Y. Jun 30 '14 at 20:12
  • \$\begingroup\$ That's not "in the Serial Monitor", that's "in response to receiving serial data". \$\endgroup\$ – Ignacio Vazquez-Abrams Jun 30 '14 at 20:16

The Serial Monitor is nothing more than a window that displays the serial data sent to the PC. Are you saying that you are passing characters to the ATMega328 and calling certain functions based on what you send?

But as for your question:

typedef void (* Caller)()

Is a typedef for a void* function pointer (with void type) with the name of Called.

Caller FuncCall[] = {a, b, c);

Will evaluate at compile-time to:

void (* Caller)() FuncCall[] = {a, b, c);

Which is essentially an array of void* function pointers.

I am not sure of what you mean by void a(), but to use function pointers you must initialize them to point to functions. Consider the following example:

void printFunc(){
    printf("Print Test\n");

int main(){
    void (*functionPtr)();
    functionPtr = &printFunc; //functionPtr points to the address of printFunc()
    return 0;

Consider the following code:

#include <SoftwareSerial.h>

/* These are function prototypes - required by the compiler */
void a();
void b();
void c();

typedef void (* Caller)();
Caller FuncCall[] = {&a, &b, &c}; //initialize addresses for pointers 
//String func_list[] = {"a","b","c"}; //this is a terrible way to do this
char func_list[] = {'a', 'b', 'c'};   //use this instead - if you wish

void setup(){

void a(){
    Serial.println("Called Function: a\n");

void b(){
    Serial.println("Called Function: b\n");

void c(){
    Serial.println("Called Function: c\n");

void loop(){
            case 'a':           //this could also be func_list[0], if you want

            case 'b':

            case 'c':

                Serial.println("There was an error\n");

Where you simply send the Arduino a character (a, b, or c) through the Serial Monitor. You can make the implementation as complicated you want. You could, for example, have string parsing and indexing into the array as suggested - but the above implementation could not get any more simple to the best of my knowledge.

  • \$\begingroup\$ Yes, I am trying to call certain functions based on what I send when I go to the Serial Monitor. In particular, the Serial Monitor outputs, "Enter a valid function to call." I'm just having trouble figuring out how exactly I can do so. \$\endgroup\$ – M.Y. Jun 30 '14 at 20:13
  • \$\begingroup\$ You can simply pass in a constant that is defined at the top of your program: #define FUNCTION_A 0xA5 for example. Loop infinintely and check the serial buffer for content and the use a switch-case, for example, to determine which code was passed in. Then call the appropriate function by using the function-pointer sytnax. \$\endgroup\$ – sherrellbc Jun 30 '14 at 20:17
  • \$\begingroup\$ I understand that pointers must be initialized to point to functions, but I am trying to call the functions outside of the code (i.e., in the text entry space in the serial monitor) \$\endgroup\$ – M.Y. Jun 30 '14 at 20:19
  • \$\begingroup\$ You are not understanding how the serial communication works. You are not interacting directly with your code, you are passing in data that must be interpreted at run-time by your code. \$\endgroup\$ – sherrellbc Jun 30 '14 at 20:20
  • \$\begingroup\$ I see. I guess my question is how can I "pass in" the data at run-time? The serial monitor tells me to "Enter a valid function to call". I apologize for being completely clueless... \$\endgroup\$ – M.Y. Jun 30 '14 at 20:23

You need to indicate that you want to access what the pointer points to.

Add an asterisk in front of the pointer and parenthesises around it.


It looks like the intent is to look up an incoming string in func_list[], turning into an index value if found, and then use that index to get the corresponding entry in FuncCall[], and call that function.

Once you have i, the index to the name of the function, the call would look like:


Also, note that you have a typo in your initializer for FuncCall — the closing paren should be a brace.

  • \$\begingroup\$ Let's say I want to call the void function "a" which is within FuncCall[] - is this possible? \$\endgroup\$ – M.Y. Jun 30 '14 at 20:38

From a comment:

I am trying to call the functions outside of the code (i.e., in the text entry space in the serial monitor)

Whoa there... you can't collect functions written in C into a buffer and then "execute" them. You'd have to build a compiler, asembler, linker etc into the Arduino program to perform their functions at runtime. C is a compiled (not an interpeted) language.

The constructs in your question merely allow you to call functions, which were available at compile time, with compliant signatures "dynamically" at runtime, by accessing them through an array. This is one way that "callback" functions are sometimes implemented.

  • \$\begingroup\$ I see. I'm wondering why then the Serial Monitor asks me to "Enter a valid function to call". I was hoping there was a way for me to "execute" a function of interest by sending the Serial Monitor a command. \$\endgroup\$ – M.Y. Jun 30 '14 at 20:39
  • \$\begingroup\$ @MichaelYom that's why there's that other array func_list... so you can compare the string you enter to a list of known functions that can be called \$\endgroup\$ – vicatcu Jun 30 '14 at 20:48
  • \$\begingroup\$ What do you mean by "comparing" to a list of known functions that can be called? So the commands that I'd enter are the strings within "func_list[]"? I am so sorry for being pretty much clueless... \$\endgroup\$ – M.Y. Jun 30 '14 at 20:52
  • \$\begingroup\$ Yes as in using something the standard strcmp function to look for string equality between the text you enter at the serial terminal and a string entry in the func_list. Your code would then call the function at the same index of the FuncCall array. \$\endgroup\$ – vicatcu Jun 30 '14 at 21:01
  • \$\begingroup\$ Ah that makes sense. Then wouldn't entering "a" execute "a" within FuncCall? Or do I need to consider all indices of func_list to call the functions at FuncCall? \$\endgroup\$ – M.Y. Jun 30 '14 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.