我需要將指針傳遞給函數,但它不起作用輸出爲空。在c編程中傳遞結構指針
我在1周前在我的大學學習這個指針,但它很混亂。
謝謝你的提前。
下面的代碼輸出:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Nooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo{
char CourseID[10];
char CourseName[50];
float score;
struct Nooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo *next;
}course;
void GetData(course *newnode,course *root,course *last)
{
char CourseID[10],CourseName[50];
float score;
FILE *input;
input = fopen("Course.txt","r");
fscanf(input,"%s %s %f",&CourseID,&CourseName,&score);
while(!feof(input))
{
newnode = (course*)malloc(sizeof(course));
strcpy(newnode->CourseID,CourseID);
strcpy(newnode->CourseName,CourseName);
newnode->score = score;
newnode->next = NULL;
if(root == NULL)
{
root = newnode;
last = newnode;
}
else
{
last->next = newnode;
last = newnode;
}
fscanf(input,"%s %s %f",&CourseID,&CourseName,&score);
}
}
void checkScore(course *run,course *root)
{
run = root;
while(run != NULL)
{
if(run->score >= 80)
{
printf("Course ID = %s\n",run->CourseID);
printf("Course Name = %s\n",run->CourseName);
printf("Your grade of this Course is = ");
if(run->score < 50)
{
printf("D");
}
else if(run->score < 60)
{
printf("C");
}
else if(run->score < 70)
{
printf("B");
}
else
{
printf("A");
}
}
run = run->next;
}
}
int main()
{
course *root = NULL,*last,*newnode,*run;
int i,cnt;
GetData(newnode,root,last);
checkScore(run,root);
return 0;
}
這是輸出
請勿張貼文字圖片! – Olaf
搜索並閱讀有關如何在c *中模擬按引用傳遞的信息。 –
[嘆氣]'GetData(newnode,root,last);'不能可靠地改變'root'的值。如初始化,它將永遠爲NULL。 –