0
I am trying to solve an exercise for the college, where the statement requests that I receive a record, with two fields as input data. A vector field for storing name and another for storing matricula. I must also declare a pointer to the heterogeneous data structure, and use this pointer when reading data and printing data on the screen.
My code does not show error in Visual Studio, but when you open the command prompt it generates the error message :
Follows my code :
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
//Programa Principal
struct cadastro {//Declaração da struct para armazenamento de registro com 2 campos
char nome[40];
int ru;
};
struct cadastro aluno, *pont1;//variavel aluno referenciando a struct
int main() {
//Titulo do programa
printf("---------Cadastro de aluno----------\n");
printf("\n");
//Entrada de informações pelo usuário
printf("Digite seu nome: ");
fflush(stdin);//Limpa dados armazenados na memória
fgets((*pont1).nome, 40, stdin);//Referencia onde na struct deve ser armazenada a informação
printf("\n");
printf("Digite seu RU: ");
scanf_s("%d", (*pont1).ru);
//Print de dados informados pelo usuário
printf("\n");
printf("---------Dados Informados-----------\n");
printf("\n");
printf("O nome digitado foi: %s\n", (*pont1).nome);
printf("\n");
printf("O RU digitado foi: %d\n", (*pont1).ru);
printf("\n");
//Fim do programa
system("pause");
return 0;
}
Everywhere I searched, I saw the use of pointers to predefined values within the code, however, I’m trying to make the same function run with the user information input.
Can someone help me?
You need to allot memory to the structure. In this case, you are pointing to nothing valid. Do at startup something like
pont1 = malloc(sizeof cadastro);
. Don’t forget to givefree(pont1)
at the end. And read about memory allocation.– Kevin Kouketsu
Remember that you can mark one as the correct answer. If you don’t know how, see here on tour and already learn a little more about the stack.
– Kevin Kouketsu