[英]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. 我试图将学生的记录存储在名为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.
我将指针变量声明为结构Student_info的一种类型,如代码中所示,并在每次我们要输入学生记录时使用malloc将内存分配给该学生变量。 I want to access the ith student and tried to enter values for that one.
我想访问第i个学生,并尝试为该学生输入值。 i tried to access ith elements using following code but it is not working properly.
我尝试使用以下代码访问ith元素,但无法正常工作。 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. 您正在
add_record()
中使用malloc()
来扩展现有分配。 This won't work. 这行不通。
You should look at realloc()
, since malloc()
will not keep the old block's data around. 您应该查看
realloc()
,因为malloc()
不会保留旧块的数据。
Also, stop casting the return value of malloc()
. 另外, 停止转换
malloc()
的返回值 。 Don't cast the return value of realloc()
either, once you've switched. 一旦切换,也不要
realloc()
转换realloc()
的返回值。
Further, having a global variable called i
is a very very bad idea. 此外,拥有一个名为
i
的全局变量是一个非常非常糟糕的主意。
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. 您的代码中存在以下几个错误:1.您在结构中没有任何名为mid_term和final_score的变量,但是您在代码中使用了它。 2.You are allocating memory for the same struct pointer twice;
2.您要为同一结构指针分配两次内存; one in main and another in add_record function.
一个主要在add_record函数中。 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.
3.您正在做的一个非常愚蠢的错误是您在打印ith学生的分数之前递增i,这就是为什么最终分数为0的原因。
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. 在这里,我假设可以在记录中添加N个学生,并且N个可以根据您的需要进行更改。这是您的工作代码。
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.