0

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;
}
user3860949
  • 115
  • 1
  • 1
  • 10
  • `int add_record(struct student_info **student)` and use `realloc` instead of `malloc`. – BLUEPIXY Oct 27 '14 at 14:40
  • possible duplicate of [How to use realloc in a function in C](http://stackoverflow.com/questions/13748338/how-to-use-realloc-in-a-function-in-c) – n. m. could be an AI Oct 27 '14 at 14:42
  • @n.m. i think that question is totally different. that was all about realloc. – user3860949 Oct 27 '14 at 15:46
  • You need to use realloc, you just don't know it yet. – n. m. could be an AI Oct 27 '14 at 15:55
  • @n.m. i used it in the code. thank you – user3860949 Oct 27 '14 at 16:23
  • these lines: i++; printf("Total_score %d\n", *&(student + i)->total_score); are incrementing the 'i' variable/offset into the student array (into an area that is not malloc'd) then trying to print items out of that area. this is undefined behaviour and can lead to a seg fault. This sequence *& cancel each other out, so should be removed. – user3629249 Oct 27 '14 at 23:58
  • this line: int i; has two problems. 1) depending on the startup code to initialize all global variables to 0, which is acceptable but poor coding technique 2) is placed in the code before all the 'types' are defined, another poor coding technique. – user3629249 Oct 28 '14 at 00:00

2 Answers2

2

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.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
0

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;
}
Aadil Ahmad
  • 139
  • 9
  • that code was to big and to simplify my question i put a small part of it and forget to removing that variables. Seriously what a silly mistake i have made, i was thinking that there may be any syntax error but the error was logical. Thank you @Aadil Ahmad. – user3860949 Oct 27 '14 at 15:41
  • there is a problem with this code.. how is the writer of the code to know that there will only be 15 students. Therefore, while I would move the realloc to add_record(), I would still have it realloc as each new student is added. – user3629249 Oct 28 '14 at 00:06
  • If that is the problem then definitely you should use realloc. The main problem you asked was how were you getting 0 you did not mentioned about the size of the students. – Aadil Ahmad Oct 28 '14 at 06:11