Storing a function pointer in an array and then calling it.

The script below stores a function pointer in an array and then calls it from the loop.
I think I defined the the WriteText declaration and the function correctly. I am not so sure how the void (*funct)(int n); in the Tst struct and the calling of the function in the loop should be coded.

An error on this line: WriteText("Hello", Test(2)); is

junk:19: error: invalid use of void expression

Additionally, calling the stored function in the loop generates an error on myArray[0].funct();:

junk:30: error: too few arguments to function

(I didn't want to put the arg here, I wanted the arg to be set when WriteText was called.

What needs to change on this?

struct tst
{
  public:
    String txt;
    void (*funct)(int n);
};

tst myArray[2];

void Test(int n)
{
  Serial.println(n);
}

void setup()
{
  Serial.begin(115200);
  WriteText("Hello", Test(2));
}

void WriteText(String txt, void (funct)(int n))
{
  myArray[0].txt = txt;
  myArray[0].funct = funct;
}

void loop()
{
  myArray[0].funct();
  delay(1000);
}

you defined the function as having an integer argument. you call it w/o any argument

I was trying to arrange things so the argument was stored in the array.

Why? Note that where you're trying to pass a function pointer, you are actually just calling test.

In a struct, elements are public by default, so your "public:" is superfluous

wildbill:
Why? Note that where you're trying to pass a function pointer, you are actually just calling test.

Myy real sketch draws a button on a touchscreen. When the button is touched, it stores the function with an arg. I want to do that so similar actions can be performed by a single function.

Example : buttons for turning on pumps = Pump1, pump2, pump3. If I can't store an argument, I will need 3 separate action functions. If I can store the arg then one action function would suffice.

superfluous "public:" deleted. Thanks.

Where are you storing the arguments?

TheMemberFormerlyKnownAsAWOL:
Where are you storing the arguments?

I was trying to store it here:

struct tst
{
  public:
    String txt;
    void (*funct)(int n);      //  for storing the function and agrument???
};

Perhaps that's not the way to do it. Maybe there could be another argument in the WriteText definition. Then I could store the function and the argument separately. When calling the function, I could plug in the argument.

I'll give that a go.

I think I'm missing the point. I'm guessing you have many buttons and want a single handler function. Which may make sense. But in the handler, you want to be able to identify the button that called it, which suggests that you'll have a switch that does different things depending on which button it was.

If that's the case, why not just call a separate function for each button that contains the necessary stuff for that button alone? Maybe have an array of button data that contains the label, the position, size, the menu screen it's associated with and the function to call.

Success! By storing the function and the argument separately, it can now pass a different argument for each button!

Thanks for the clues that led me here.
Here is the code

struct tst
{
  String txt;
  int arg;
  void (*funct)(int n);
};

tst myArray[2];

void Test(int n)
{
  Serial.println(n);
}

void setup()
{
  Serial.begin(115200);
  WriteText("Hello", 3, Test);
}

void WriteText(String txt, int arg, void (funct)())
{
  myArray[0].txt = txt;
  myArray[0].arg = arg;
  myArray[0].funct = funct;
}

void loop()
{
  Serial.println(myArray[0].txt);
  myArray[0].funct(myArray[0].arg);
  delay(1000);
}