I made a struct like this:
typedef struct {
int color[3];
int positions[4];
char init[20];
void (*fn)();
} buttons;
and a variable like this:
button test[1] =
{
{{0,0,0},{0,0,100,100},"getSomething",setSomething}
}
In loop() I call test[i].color, test[i].position normally.
Problems start when I want to execute a function.
I made two attemps, one with string and one with a function statement. With the string I have no problems using a strcmp() but it's not what I want.
I need to know how I can store 2 different functions in the struct and how I can execute.
Thanks in advance!
-
Could you post what you've tried, please, and in what way it didn't work?Mark Smith– Mark Smith2017年03月07日 21:41:43 +00:00Commented Mar 7, 2017 at 21:41
-
Jot answers is what I needed!onlygio– onlygio2017年03月08日 00:36:32 +00:00Commented Mar 8, 2017 at 0:36
-
What mark was saying is if you post more code people may be able to understand what you are trying to say better and help you faster. I thought you wanted to store two function pointer in the one struct by what your text says.Code Gorilla– Code Gorilla2017年03月09日 09:00:49 +00:00Commented Mar 9, 2017 at 9:00
1 Answer 1
The typedef is no longer needed, using the 'struct' with a name declares the type.
// Arduino Uno
struct buttons
{
int color[3];
int positions[4];
char init[20];
void (*fn)();
};
// Declare the functions here, or use prototyping
void func1();
void func2();
buttons test[] =
{
{ {0,0,0}, {0,0,100,100}, "getSomething", func1 },
{ {40,40,40}, {50,50,10,10}, "somethingElse", func2 },
};
void setup()
{
Serial.begin(9600);
Serial.println("Calling the functions:");
test[0].fn();
test[1].fn();
}
void loop()
{
}
void func1()
{
Serial.println("func1");
}
void func2()
{
Serial.println("func2");
}
-
Not needed because what? Because it is C++?Peter Mortensen– Peter Mortensen2020年01月13日 19:48:31 +00:00Commented Jan 13, 2020 at 19:48