Ticker::attach_ms does not support structures pointers ?

Hello duiners,

I have a question about the library Ticker.h but, first of all, I want you to know I'm dev'ing on an ESP8266. I know the libraries aren't the same but it looks like there are very similar.

I need to pass an argument to a callBack function of a ticker using but this argument MUST be a pointer to a structure.
It compiles with no error or warning about it but When I print a value pointed by my arument, the value does not make any sense. Here an exemple code.

#include <Ticker.h>

struct curmean{
  int mean;
  int n;
  int N;
};

Ticker T;

void test0(curmean* src){
    Serial.println(src->N);
}
void test(){
    curmean* arg = {1, 2, 3};
    T.attach_ms(500, test0, &arg);
}

void setup(){
    Serial.begin(115200);
    while(!Serial);
    test();
}

void loop(){}

output:
100
100
100
100
100
...

do you know how I can manage to pass this structure to the callback function without pointers and global variables (meh)?

thanks for your help :3

void test(){
    curmean* arg = {1, 2, 3};
    T.attach_ms(500, test0, &arg);
}

You are creating a local variable (arg) which is a pointer to a structure. That local variable goes out of scope as soon as you return from 'test()' so using the address of that pointer as a pointer to a structure won't work.

Try this:

void test(){
    static curmean arg = {1, 2, 3};
    T.attach_ms(500, test0, &arg);
}

This makes the structure static so it doesn't go out of scope and passes the address of the structure, not the address of a pointer to the structure.