I am trying to store record of students in a pointer variable named as student. i declared that pointer variable as a type of struct student_info as shown in code and assigned memory to the student variable using malloc whenever we want to enter a student record. I want to access the ith student and tried to enter values for that one. i tried to access ith elements using following code but it is not working properly. whenever i attemp to print value stored in student variable it always shows zero. please tell me the mistake in this code and is my way of accessing the element is right or not.
#include<stdio.h>
#include<stdlib.h>
int i;
struct student_info
{
int id;
char name[20];
int quiz1;
int quiz2;
int total_score;
};
int choice();
int add_record(struct student_info *);
int main()
{
int option;
struct student_info *student;
student = (struct student_info *) malloc(sizeof(struct student_info));
while (1)
{
option = choice();
if (option == 1)
{
add_record(student);
}
else
{
exit(0);
}
}
return 0;
}
int choice()
{
int option;
printf("------------------------------------\n");
printf("------------------------------------\n");
printf("\t\tMenu\t\t\n");
printf("------------------------------------\n");
printf("------------------------------------\n");
printf("1. Add student record\n");
printf("0. exit\n")
printf("Enter your choice\n");
scanf("%d", &option);
return option;
}
int add_record(struct student_info * student)
{
if (i != 0)
{
student = (struct student_info *) malloc(sizeof(struct student_info) * (i + 1));
}
(student + i)->id = i;
printf("enter student name");
scanf("%s", (student + i)->name);
printf("Enter quiz 1 marks");
scanf("%d", &(student + i)->quiz1);
printf("Enter quiz 2 marks");
scanf("%d", &(student + i)->quiz2);
(student+i)->total_score = (student +i)->quiz1 + (student+i)->quiz2;
i++;
printf("Total_score %d\n", *&(student + i)->total_score);
return 0;
}