1

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!

3
  • Could you post what you've tried, please, and in what way it didn't work? Commented Mar 7, 2017 at 21:41
  • Jot answers is what I needed! Commented 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. Commented Mar 9, 2017 at 9:00

1 Answer 1

2

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");
}
1
  • Not needed because what? Because it is C++? Commented Jan 13, 2020 at 19:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.