Problems with reading the struct in function

0

For several days I've been trying to understand why this error occurs, but I have not found it anywhere. It is as follows, I declared a vector of the data struct and passed that vector to the function that receives it with a pointer type parameter, however, when I am going to read the data with the special operator - > gives the following error, "invalid type argument of '->', could anyone 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);
    }
}
    
asked by anonymous 13.05.2017 / 19:33

2 answers

0

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

When writing:

&dados[count]->avenida

You are trying to get the avenue attribute of dados[count] , which is Data type, not Data pointer.

An alternative is to use the . operator:

&dados[count].avenida

Or put paranteses to define the order:

&(&dados[count]).avenida

Next you will probably get another error because you are trying to read avenue of Data type that do not have this attribute, I think in the end, a line of yours should look something like:

gets(dados[count].endereco.avenida);
    
13.05.2017 / 20:31
0

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

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

The gets function is also not recommended for reading the text, so you use scanf with the "%[^\n]" parameter or the fgets function.

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

&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 it 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
    }
}
    
13.05.2017 / 23:38