[英]how to access value of a pointer variable declared as type of struct
我試圖將學生的記錄存儲在名為student的指針變量中。 我將指針變量聲明為結構Student_info的一種類型,如代碼中所示,並在每次我們要輸入學生記錄時使用malloc將內存分配給該學生變量。 我想訪問第i個學生,並嘗試為該學生輸入值。 我嘗試使用以下代碼訪問ith元素,但無法正常工作。 每當我嘗試打印存儲在學生變量中的值時,它始終顯示為零。 請告訴我這段代碼中的錯誤,這是我訪問元素的方法正確與否。
#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;
}
您正在add_record()
中使用malloc()
來擴展現有分配。 這行不通。
您應該查看realloc()
,因為malloc()
不會保留舊塊的數據。
另外, 停止轉換malloc()
的返回值 。 一旦切換,也不要realloc()
轉換realloc()
的返回值。
此外,擁有一個名為i
的全局變量是一個非常非常糟糕的主意。
您的代碼中存在以下幾個錯誤:1.您在結構中沒有任何名為mid_term和final_score的變量,但是您在代碼中使用了它。 2.您要為同一結構指針分配兩次內存; 一個主要在add_record函數中。 3.您正在做的一個非常愚蠢的錯誤是您在打印ith學生的分數之前遞增i,這就是為什么最終分數為0的原因。
在這里,我假設可以在記錄中添加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.