Request for Member 'name' in Something not a Structure or Union - Error

Asked

Viewed 559 times

0

#include <stdio.h>
#define MAX_VETOR 6

const int na = 1;

typedef struct {
    char nome[10];
    char sexo[1];
} tp_pessoa;

typedef struct {
    tp_pessoa *pessoas[MAX_VETOR];
    int inicio,fim;
} tp_vetor;

int inicializa_vetor(tp_vetor *V){
V->fim = 0;
V->inicio = 0;
}

int insere_vetor(tp_vetor *V, tp_pessoa *p){
inicializa_vetor(V);


if (V->fim <= MAX_VETOR)
{
    V->pessoas[V->fim]= p;
    (V->fim)++;

    return 1;

}
else{
    printf("bbbb");
    return 0;

}
}

main() {
int i;
tp_pessoa *p[MAX_VETOR];
tp_vetor *V;
int ret;
FILE *arq;
char nomearq[] = "macho.txt";
arq = fopen(nomearq, "w+t");

for (i=0; i<=6;i++){
    printf("Digite o %do. nome: ",i); 
    scanf("%s",&p->nome);//aqui acontece o erro
    printf("Digite o sexo: "); 
    scanf("%s",&p->sexo);//Aqui se repete
}


insere_vetor(&V,p);

for(i=0;i<=6;i++){
    printf("Nome = %s\t",V->pessoas[i]->nome);
    printf("Sexo = %s \n",V->pessoas[i]->sexo);
}
}

Would anyone know the reason for this mistake? It repeats itself in sex reading..

  • Note that your variable V is an address for a tp_vector structure but at no time do you allocate memory for the structure pointed by V.

  • The idea is to create a static list without dynamic allocation (teacher that required)

  • Then statically declare the memory allocated to your structure. It is not enough to just declare a pointer that points to nothing.

  • How could I do it?

  • Instead of tp_vector *V; put tp_vector V;. Idem for tp_person *p[MAX_VETOR]; where you place tp_person p[MAX_VETOR];. Note that in your tp_vector structure you allocate space for an array of pointers for tp_person and not an array of tp_person. Give a good study on pointers. The impression is that you copied the program but do not know the concepts.

  • could you at least put the number of the line where the error occurred ??? otherwise we will need to analyze the entire program, to try to deduce where it is going wrong...

Show 1 more comment

1 answer

0

//scanf("%s",&p->nome);
scanf("%9s", p[i]->nome);

Note that the sexo no room for the terminator ('\0') string.

Browser other questions tagged

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