How to create an Index of functions

Hello everyone this is my first time posting on a forum ever, but I ran into an issue that in which i find myself stuck. I don't know if this is possible, but I am trying to find a way to make an index of functions or some way to call a function within a structure that I made.

The specifics of this program will be taking an input from the comm port. Then it will scan through all of the letters that I have initiated within a struct type. This will then run a void function that will then output something on the outpin section. see bellow for a simple program that i wrote.

typedef Strcuct cntrl {
char action[];
Char keypress;
(place for function name);
Int outpin;
};
int amt = 2;

//Set
Cntrl control[amt]={
("Left forward", ‘w’, test(),22),
("Kill switch 2nd control", ‘p’, testn(), 1)
};

void setup()
{
Serial.begin(9600);
}

void loop()
{
Val = Serial.read();
scan();
}

scan(){
For(int i=0; i<amt; i++)
{
If(control*.keypress==Val)*

  • {*
    _ Control*.function*_
    * }*
    }
    }

Hello and welcome,

Search about function pointers

Try reading:

Hi, welcome to the forum.

This is a great tutorial : Gammon Forum : Electronics : Microprocessors : Function pointers / function callbacks / function variables

Did you know that the keyword "typedef" is no longer needed for a struct ?

// Create a struct type:
struct myData_STRUCT
{
  int x;
  byte b;
};

myData_STRUCT myData[2] = { {10,1}, {11,2} };

Peter_n:
Did you know that the keyword "typedef" is no longer needed for a struct ?

It was never "needed". typedef has always been an optional modifier.

Regards,
Ray L.

Well, after tons of research yesterday including diving into a couple books I had from school, I came up with the same conclusion of using function pointers. I'm glad that I'm on the rite track. Thank you so much Peter_n for the link I think I'll go through what it says and get back to you all and let you know what happens.

I didn't know that the typedef optional, I'll make some modifications to my program then. Also, Peter, I noticed you used {} instead of () to define myData[], does that work. Just an FYI I'm actually really new to Arduino programming and I'm trying to build a robot for the Lunabotics comp. as my capstone project, so you all are helping me out immensely.

When you define a data item that is an array, any list of initial values appears within brackets. So, if you have an integer array and want the first 5 values to be 10, 20, 30, 40, 50, you'd use:

int values[] = {10, 20, 30, 40, 50};

Note that the compiler automatically sizes the array for you to hold the list. If you need 20 values, of which only the first five are known, you can also use:

int values[20] = {10,20, 30, 40, 50};

which creates the array with 20 elements and sets everything after the 5th element to 0. If you don't need to initialize any of the values, then just use int values[20];

Okay I was able to read the link that you guys gave me and tried to test it out. It seems to work as a pointer, but there is still a problem, I think there still is a problem. It keeps printing out "«ÿ" I'm guessing this is just some crap that's in the register. Bellow is my code, what have I done wrong?

typedef void (*func) ();//make a function pointer type *func
struct cntrl{
char action[];
char keyPress;
func function;
int outPin;
boolean toggle;
};

cntrl control[] =
{
("this works", 'w', test(), 4, false)
};

void setup()
{
Serial.begin(9600);
}

void loop()
{
control[0].function;
}
//test every possible serial output
void test()
{
Serial.println("hello") ;
Serial.print("hello");
Serial.println(control[0].action)
Serial.print(control[0].action)
}

You define a struct with char array "action" of unknown size. How would the compiler know what to do ?

A struct is a bundle of variables in a memory location. It is not possible that one struct is larger than the other of the same struct type.

I can't test you sketch, I get many compiler errors. Can you show your sketch between code tags ?
Read number 7 on this page about code tags : http://forum.arduino.cc/index.php/topic,148850.0.html

A couple of things: 1) Please read Nick Gammon's two posts at the top of this Forum about how to post, especially how to use code tags when posting code, 2) you were very close, but using the function pointers is a little different than you used it. See below:

typedef void (*func) ();//make a function pointer type *func
struct cntrl {
  char action[20];    // You need a size here
  char keyPress;
  func function;
  int outPin;
  boolean toggle;
};

cntrl control[] =
{
  {"this works   ", 'w', test, 4, false}
};

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  control[0].function();   // Make it a function call...
}

//test every possible serial output
void test()
{
  Serial.print("hello  ") ;
  Serial.print(control[0].action);
  Serial.print("  ");
 Serial.print(control[0].keyPress);

}

okay you have a good point, I was able to compile and upload this code but I will try bounding the "action" part. Thank you for the pointers, the first code was something that I came up with quickly, I didn't test it but this one works and compiles.

typedef void (*GeneralFunction) ();

void hello ()
  {
  Serial.print ("hello");
  }  // end of hello
  
void world ()
  {
  Serial.print ("world");
  }  // end of world

GeneralFunction foo;

void setup ()
  {
  Serial.begin (9600);
  
  foo = hello;
  foo ();
  
  foo = world;
  foo ();
  }  // end of setup

void loop () { }

GREAT!! thank you that worked. okay I will make sure that i bound these. Thank you so much.

RayLivingston:
It was never "needed". typedef has always been an optional modifier.

Regards,
Ray L.

It's needed in C to create an alternative name for a struct without using "struct" before each use of the name:

typedef struct foo {
    // ...
} foo;

With that you can use "foo" or "struct foo" interchangeably.

C++ (which Arduino uses) automatically creates a type that you can use without "struct" before the name. The same also applies to "class" (which C doesn't have), "union", and "enum".

But otherwise it's not needed when only creating a struct.

christop:
It's needed in C to create an alternative name for a struct without using "struct" before each use of the name:

Yes, if you want to define a new type (hence the name typdef), then you use it. It was never mandatory on struct definitions. It is a modifier that defines a new type based on the defnition of the struct (or other data type).

Regards,
Ray L.