1
I cannot assign value to a variable of a pointer struct
within a struct
.
My structs
:
typedef struct notas{
float geral;
float especifica;
}Notas;
typedef struct data{
int dia,mes,ano;
}Data;
typedef struct local{
char ender[81];
int sala;
}Local;
typedef struct candidatos{
int inscr;
char nome[81];
Local *loc;
Data nasc;
Notas nota;
}Candidatos;
And the code that should assign values:
void ler_candidatos(Candidatos *A, int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Digite numero de inscriçao: ");
scanf("%d",&A[i].inscr);
fflush(stdin);
printf("Digite o nome: ");
scanf("%[^\n]",A[i].nome);
fflush(stdin);
printf("Digite o endereço: ");
scanf("%[^\n]",A[i].loc->ender); //erro aqui.
fflush(stdin);
printf("Digite a sala: ");
scanf("%d",&A[i].loc->sala);
fflush(stdin);
printf("Digite sua data de nascimento: ");
scanf("%d %d %d",&A[i].nasc.dia,&A[i].nasc.mes,&A[i].nasc.ano);
fflush(stdin);
printf("Digite sua nota geral: ");
scanf("%f",&A[i].nota.geral);
fflush(stdin);
printf("Digite sua nota especifica: ");
scanf("%f",&A[i].nota.especifica);
fflush(stdin);
}
}
How is being allocated the object that is received in the parameter
A
? Whenever you have a type that a pointer to something you will have to allocate space for that something, since what will be saved in that variable or member of a structure is just the pointer and not the die. In general you will have to use amalloc()
for this.– Maniero
Hello user18400, Welcome to Stack Overflow :) I separated your code in two to avoid the scroll bar, which disturbs a little the code reading #Ficaadica.
– Oralista de Sistemas
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero