define a struct variable

Hello friends, I'm struggling to create a variable "test" in the attached code.
I created a type named "t_denree" including many fields, and set a variable test. But when I want to assigne a value for one of the fields of my variable test using this instruction: "test.id = 0;" the compiler returns: ""test" does not name a type".

I do not see any mistakes in my definition of the type "t_denree" and of the variable "test".

Hope you can help me out !

Thanks a lot.

types_h.ino (510 Bytes)

nicox178:
Hello friends, I'm struggling to create a variable "test" in the attached code.
I created a type named "t_denree" including many fields, and set a variable test. But when I want to assigne a value for one of the fields of my variable test using this instruction: "test.id = 0;" the compiler returns: ""test" does not name a type".

I do not see any mistakes in my definition of the type "t_denree" and of the variable "test".

Hope you can help me out !

Thanks a lot.

you incorrectly declared the struct / use typedef.

This is how it should be IMHO:

(Compiles, not tested!)

typedef struct {
  char nom[20];
  int id;
  float quantite;
  float prix_kilo;
  float debit_nominal; //massique
  char date[12]; // J/M/AN
} t_denree ;

t_denree test{"pate", 0, 10, 5, 0.05, "01/02/2020"}; //this is how you would initialise 'test'

void setup() {

  Serial.begin(115200);
  //if you wanted to initialise 'test' inside 'setup()'
  //test.nom = "pate"
  //test.id = 0;
  //test.quantite = 10;
  //test.prix_kilo = 5;
  //test.debit_nominal = 0,05;
  //test.date = "01/02/2020";

  Serial.print("nom: ");
  Serial.println(test.nom);
  Serial.print("id: ");
  Serial.println(test.id);
  Serial.print("quantite: ");
  Serial.println(test.quantite, 2);
  Serial.print("prix_kilo: ");
  Serial.println(test.prix_kilo, 2);
  Serial.print("debit_nominal: ");
  Serial.println(test.debit_nominal);
  Serial.print("date: ");
  Serial.println(test.date);
}

void loop() {

}

hope that helps....

(most of this is pointed out in the previous reply)

You cannot have an assignment outside of a function.

If you want to initialize the structure, you can do this:

t_denree test = {"pate", 0, 10, 5, 0.05, "01/02/2020"};

There is a problem with your declaration of the struct, nom and date are declared as char, which is a single character. Either declare those variables as char * or as char arrays if you want to store a string of characters.

Print will not work with an entire struct, you will have to print the individual elements of the array. Printf is not implemented on the Arduino.

Thank you very much for your help ! It solved my problem