I wrote below code for reading/printing the structure using pointer. The application is printing the data, but later crashes. Seems to be some silly mistake somewhere. Unable to find the exact problem. I tried using pass by value in printStruct and returning structure in readStruct. There is no issue. When I use pointer not sure what is going wrong.
#include<stdio.h>
#include <inttypes.h>
#include <string.h>
struct student{
char USN[10];
char name[50];
char gender;
uint8_t age;
};
void printStruct (struct student*);
void readStruct(struct student*);
int main() {
uint8_t numStudents;
printf("Enter number of students\n");
scanf("%d",&numStudents);
struct student firstSemStudent[numStudents];
readStruct(firstSemStudent+0);
printStruct(firstSemStudent+0);
return 0;
}
void printStruct (struct student *var1)
{
printf("USN= %s\n",var1->USN);
printf("Name = %s\n",var1->name);
printf("Age = %d\n",var1->age);
printf("Gender = %c\n",var1->gender);
}
void readStruct(struct student *temp)
{
printf("\nEnter USN: ");
fflush(stdin);
gets(temp->USN);
printf("\nEnter Name: ");
fflush(stdin);
gets(temp->name);
printf("\nEnter age\n");
scanf("%d",&temp->age);
printf("\nEnter the Gender as M/F");
fflush(stdin);
scanf("%c",&temp->gender);
}