C语言结构体问题?

#include<stdio.h>
#define NULL 0
struct student
{
int num;
float score;
struct student *next;
}stu[3]={{234,333.3},{23,54.33},{255,23.66}};
void main()
{
struct student *head;

head=&stu[0];
stu[0].next=&stu[1];
stu[1].next=&stu[2];
stu[2].next=NULL;
while(*head)
{
printf("%d,%f",head->num,head->score);
head=head.next;
}
}
错在哪里?如何在此基础上改正?
谢谢!

#include<stdio.h>

struct student
{
int num;
double score;
struct student *next;
}stu[3]={{234,333.3,NULL},{23,54.33,NULL},{255,23.66,NULL}};

void main()
{
struct student *head;

head=&stu[0];
stu[0].next=&stu[1];
stu[1].next=&stu[2];
stu[2].next=NULL;
while(head)
{
printf("%d,%f\n",head->num,head->score);
head=head->next;
}
}
总结:1.指针最好赋值为NULL
2.while循环中*head出错,应该是判断指针是否为空,而不是判断指针所指的值!
3.head.next出错,head为指针,应该用head->next;
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-08-04
#include<stdio.h>
struct student
{
int num;
float score;
struct student *next;
}stu[3]={{234,333.3},{23,54.33},{255,23.66}};
void main()
{
struct student *head;

head=&stu[0];
stu[0].next=&stu[1];
stu[1].next=&stu[2];
stu[2].next=NULL;
while(head!=NULL)//*head switch to head!=NULL
{
printf("%d,%f",head->num,head->score);
head=head->next;//. switch to ->
}
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网