如何获取指定为结构类型的指针变量的值

如何获取指定为结构类型的指针变量的值

问题描述:

我想将学生的记录存储在名为student的指针变量中。 我将指针变量声明为结构student_info的类型,如代码中所示,并且只要我们想输入学生记录就使用malloc将内存分配给student变量。我想访问第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; 
} 
+0

'int add_record(struct student_info ** student)'并使用'realloc'而不是'malloc'。 – BLUEPIXY 2014-10-27 14:40:48

+0

[如何在C中使用realloc函数]的可能的重复(http://stackoverflow.com/questions/13748338/how-to-use-realloc-in-a-function-in-c) – 2014-10-27 14:42:33

+0

@ n.m。我认为这个问题是完全不同的。这完全是关于realloc。 – user3860949 2014-10-27 15:46:55

代码中有几个错误,如下所示: - 1.您的结构中没有任何名为mid_term和final_score的变量,但是您在代码中使用了它。 2.您为同一个结构指针分配两次内存;一个在main中,另一个在add_record函数中。 3.你正在做的非常愚蠢的错误是你打印前ith学生的分数,这就是为什么你得到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; 
} 
+0

该代码是大和简化我的问题,我把它的一小部分,并忘记去除这些变量。说真的,我犯了一个愚蠢的错误,我以为可能有语法错误,但错误是合乎逻辑的。谢谢@Aadil Ahmad。 – user3860949 2014-10-27 15:41:52

+0

这个代码有问题。代码的作者如何知道只有15名学生。因此,虽然我会将realloc移到add_record(),但我仍然会在添加每个新学生时重新分配它。 – user3629249 2014-10-28 00:06:55

+0

如果这是问题,那么肯定你应该使用realloc。 你问的主要问题是你如何得到0你没有提到学生的大小。 – 2014-10-28 06:11:09

您正在使用malloc()add_record()看似增长的现有分配。这不起作用。

你应该看看realloc(),因为malloc()不会保留旧块的数据。另外,stop casting the return value of malloc()。一旦切换,不要投入realloc()的返回值。

此外,拥有一个名为i的全局变量是一个非常糟糕的主意。

+0

谢谢@unwind。 – user3860949 2014-10-27 15:38:20