简体   繁体   中英

how to access value of a pointer variable declared as type of struct

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;
}

You are using malloc() in add_record() to seemingly grow an existing allocation. This won't work.

You should look at realloc() , since malloc() will not keep the old block's data around.

Also, stop casting the return value of malloc() . Don't cast the return value of realloc() either, once you've switched.

Further, having a global variable called i is a very very bad idea.

There are several mistakes in your code which are as follow:- 1.You dont have any variable named mid_term and final_score in your struct but you are using it in your code. 2.You are allocating memory for the same struct pointer twice; one in main and another in add_record function. 3. The very silly mistake you are doing is you are incrementing i before printing ith student's score thats why you are getting 0 as final score.

Here I am assuming that there are N number of students which can be added in the record and N can change according to your need.Here is your working code.

#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,N=15;
struct student_info *student;
student = (struct student_info *)malloc(sizeof(struct student_info)*N);
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)
{

(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;

printf("Total_score %d\n",*&(student+i)->total_score);
i++;
return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM