Error in Arduin (pointer to struct)

Asked

Viewed 75 times

1

I’m trying to make the definition of types below but the Arduino accuses error of type declaration.

/*Estrutura que abstrai a seringa*/
struct {
  int pot[7];
  float nivel[7] = {0, 0.5, 1, 1.5, 2, 2.5, 3};
} seri;

/*Definição do tipo de dado abstrato Seringa*/
typedef struct seri Seringa;

/*Instanciação da variável seringa (minúsculo) do tipo Seringa (maiúsculo)*/
Seringa *seringa;

seringa = malloc(sizeof(struct seri));

*Arduino: 1.8.10 (Linux), Board: "Arduino/Genuino Uno" syringe:18:1: error: 'syringe' does not name a type syringe = malloc(sizeof(struct Seri));

2 answers

0

Try to remove the typedef struct

/*Estrutura que abstrai a seringa*/
struct {
  int pot[7];
  float nivel[7] = {0, 0.5, 1, 1.5, 2, 2.5, 3};
} seri;

/*Definição do tipo de dado abstrato Seringa*/
seri Seringa; /* <- aqui */

/*Instanciação da variável seringa (minúsculo) do tipo Seringa (maiúsculo)*/
Seringa *seringa;

seringa = malloc(sizeof(struct seri));
  • Keeps making the same mistake.

0

You did not correctly define the name Seringa.

The correct way is:

typedef struct {
  int pot[7];
  float nivel[7] = {0, 0.5, 1, 1.5, 2, 2.5, 3};
} Seringa;

Seringa *seringa;

seringa = malloc(sizeof(Seringa));

Or:

struct Seringa{
  int pot[7];
  float nivel[7] = {0, 0.5, 1, 1.5, 2, 2.5, 3};
};

typedef struct Seringa Seringa;

Seringa *seringa;

seringa = malloc(sizeof(Seringa));

The typedef then it serves only to avoid writing all the time struct each time it refers to the type of the structure. It could be done this way, without using typedef:

struct Seringa{
  int pot[7];
  float nivel[7] = {0, 0.5, 1, 1.5, 2, 2.5, 3};
};

struct Seringa *seringa;

seringa = malloc(sizeof(struct Seringa));

Also it is not possible to call a function in the global scope. The call of malloc should be done within another function, for example within the function setup of the Arduino API.

  • In both cases still giving error..

  • Although I didn’t test, I am convinced that there is no error there. Can send a link to the complete code?

  • https://docs.google.com/document/d/1_fR4Sij5vfxUFCdjaEUhpy6ZJhTSc5TN9YjfxEcK-mg

  • You are calling the function malloc in the glogal scope, it is not possible to do this in C. The pointer seringa should be initialized within a function, for example within setup.

  • Hmmm, I didn’t know that. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.