2014-10-27 31 views
0

我想將學生的記錄存儲在名爲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

回答

0

代碼中有幾個錯誤,如下所示: - 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

2

您正在使用malloc()add_record()看似增長的現有分配。這不起作用。

你應該看看realloc(),因爲malloc()不會保留舊塊的數據。另外,stop casting the return value of malloc()。一旦切換,不要投入realloc()的返回值。

此外,擁有一個名爲i的全局變量是一個非常糟糕的主意。

+0

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

相關問題