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.
– anonimo
The idea is to create a static list without dynamic allocation (teacher that required)
– B. Fernandes
Then statically declare the memory allocated to your structure. It is not enough to just declare a pointer that points to nothing.
– anonimo
How could I do it?
– B. Fernandes
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.
– anonimo
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...
– zentrunix