Aiuto con la variabile struct

Ho creato una struttura come questa:

typedef struct { int color[3]; int positions[4]; char init[20]; void (*fn)(); } buttons; 

e un variabile come questa:

button test[1] = { {{0,0,0},{0,0,100,100},"getSomething",setSomething} } 

In loop () chiamo test [i] .color, test [i] .position normalmente.

I problemi iniziano quando voglio eseguire una funzione.

Ho fatto due tentativi, uno con una stringa e uno con unistruzione di funzione. Con la stringa non ho problemi a usare strcmp () ma non è quello che voglio.

Ho bisogno di sapere come posso memorizzare 2 diverse funzioni nella struttura e come posso eseguire.

Grazie in anticipo!

Commenti

  • Potreste pubblicare ciò che ' ve provato, per favore, e in che modo ' t ha funzionato?
  • Jot risposte è ciò di cui avevo bisogno!
  • Che segno stava dicendo è che se inserisci più codice le persone potrebbero essere in grado di capire meglio cosa stai cercando di dire e aiutarti più velocemente. Pensavo volessi memorizzare due puntatori a funzione in una struttura in base a ciò che dice il tuo testo.

Risposta

Il typedef non è più necessario, luso della “struct” con un nome dichiara il tipo.

// 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"); } 

Commenti

  • Non necessario perché cosa? Perché è C ++?

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *