Problems with struct reading in function

Asked

Viewed 83 times

0

For several days I’ve been trying to understand why this error occurs, but I haven’t found it anywhere. The following is, I declared a vector of the data struct and passed this vector to the function that receives it with a pointer parameter, however, when I will read the data with the special operator -> gives the following error, "invalid type argument of '->', someone could help me with this?

Relate the item

->'

# include stdio.h

typedef struct {
    char avenida[70];
    char bairro[70];
    int numero;
    char complemento[40];
    char cidade[30];
    char uf[2];
    long int cep;

} Endereco;


typedef struct {
    char nome[50];
    int telefone[3];
    Endereco endereco;

} Dados;

Dados dados[5];

void ler_dados(Dados *dados, int tamanho);

int main() {

    int count;

    ler_dados(dados, 5);

    return 0;

}

void  ler_dados(Dados *dados, int tamanho) {
    int count;

    for(count=0; count<tamanho; count++) {
        printf("Usuário %d\n", count+1);

        gets(&dados[count]->avenida);
        gets(&dados[count]->bairro);
        scanf("%d", &dados[count]->bairro);
        gets(&dados[count]->complemento);
        gets(&dados[count]->cidade);
        scanf("%s" &dados[count]->uf);
        scanf("%ld", &dados[count]->cep);
    }
}

2 answers

0

The operator -> expects the element on the left to be a pointer.

When writing:

&dados[count]->avenida

You’re trying to catch the avenue attribute of dados[count], which is of the type Dice, not Data pointer.

An alternative is to use the operator .:

&dados[count].avenida

Or put paranteses to define the order:

&(&dados[count]).avenida

Then you will probably get another mistake by trying to read avenue type Data that do not have this attribute, I think at the end, a line your should look something like:

gets(dados[count].endereco.avenida);

0

Your code has many syntax errors. first to import the stdio.h, must have the characters < and > to define the library that is importing: include <stdio.h>.

Its function ler_dados has a type parameter Dados and the structure Dados has only the fields nome, telefone and endereco, So it is not possible to read the way it is being done.

The function gets is also not recommended to read the text, for this we use the scanf with the parameter "%[^\n]" or the function fgets.

In C pointers are also vectors, and when trying to access a pointer position, it ceases to be a pointer, and when using the & in a structure with the attribute, the & act on the last item of the ex line:

&dados[0]; // retorna o endereço de memória de dados[0]
&dados[0].nome // retorna o endereço de memória de nome dentro de dados
(&dados[0])->nome // retorna o endereço de memória de dados[0] e pega o nome

So the way your code is structured is wrong.

Wrong way:

gets(&dados[count]->avenida);
gets(&dados[count]->bairro);
scanf("%d", &dados[count]->bairro);
gets(&dados[count]->complemento);
gets(&dados[count]->cidade);
scanf("%s", &dados[count]->uf);
scanf("%ld", &dados[count]->cep);

And the right way to do the structuring would be:

Right way:

void  ler_dados(Dados *dados, int tamanho) {
    int count;

    for(count=0; count<tamanho; count++) {
        printf("Usuário %d\n", count+1);

        scanf("%[^\n]",dados[count].endereco.avenida);
        scanf("%[^\n]",dados[count].endereco.bairro);
        scanf("%[^\n]",dados[count].endereco.complemento);
        scanf("%[^\n]",dados[count].endereco.cidade);
        scanf("%s", dados[count].endereco.uf);
        scanf("%ld", &dados[count].endereco.cep); # aqui é pego o endereço de memória da variável cep dentro de endereco
    }
}

Browser other questions tagged

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