0

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 Mar 7 '17 at 21:41
  • Jot answers is what I needed! – onlygio Mar 8 '17 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 Mar 9 '17 at 9:00
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");
}

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.