| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 791 人关注过本帖
标题:菜鸟求助,请各位大小虾帮忙看一下哪里出问题了~
只看楼主 加入收藏
gmtianxia
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-5-17
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:7 
菜鸟求助,请各位大小虾帮忙看一下哪里出问题了~
原文,这是一个建立链表的小程序:出了很多警告和一个错误,麻烦各位大大办忙看看并且解释一下存在的问题,感激不尽!

#include<stdio.h>
#include<malloc.h>
#define NULL 0;
struct student
{
    int age;
    char name[20];
    float score;
    struct student *next
};
void main()
{
    int i;
    struct student *p,*head,*p1;
    for(i=0;i<3;i++)
    {
        if(i=0)head=p1=p=(struct student *)malloc(sizeof(struct student));
        else
        {
        p=(struct student *)malloc(sizeof(struct student));
        if(i=1)head->next=p1=p;
        if(i=2){p1->next=p;p->next=NULL;}
        }
    printf("please input the student information:\n");
    scanf("%20s,%3d,%5f",&p->name,&p->age,&p->score);
    p=head;
    };
   
   
    do
    {printf("%s's age is %d, score is%f",p->name,p->age,p->score);
    p=p->next;
    }while(p!=NULL);
   
}





提示的错误信息:

gcc -Wall -c "node.c" (在目录/home/gmtianxia/桌面/编程练习中)
编译失败
node.c:3:1: warning: "NULL" redefined
In file included from /usr/include/malloc.h:25,
                 from node.c:2:
/usr/lib/gcc/i486-linux-gnu/4.4.1/include/stddef.h:400:1: warning: this is the location of the previous definition
node.c:10: warning: no semicolon at end of struct or union
node.c:11: warning: return type of ‘main’ is not ‘int’
node.c: In function ‘main’:
node.c:17: warning: suggest parentheses around assignment used as truth value
node.c:21: warning: suggest parentheses around assignment used as truth value
node.c:22: warning: suggest parentheses around assignment used as truth value
node.c:25: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’
node.c:33: error: expected ‘)’ before ‘;’ token
搜索更多相关主题的帖子: 小虾 
2010-05-17 05:21
冥卫
Rank: 8Rank: 8
来 自:深山老林
等 级:蝙蝠侠
帖 子:280
专家分:772
注 册:2010-4-20
收藏
得分:0 
帮忙顶顶
2010-05-17 08:31
gmtianxia
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-5-17
收藏
得分:0 
谢谢楼上帮顶,继续求助大虾们~
2010-05-17 08:59
gmtianxia
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-5-17
收藏
得分:0 
发现  if(i=0) 错了,应该为if(i==0) 郁闷~     但是后面的scanf("%20s,%3d,%5f",&p->name,&p->age,&p->score);和  do
    {printf("%s's age is %d, score is%f",p->name,p->age,p->score);
    p=p->next;
    }while(p!=NULL);

还是看不出问题, 各位大小虾请指教

2010-05-17 09:23
yc2575757
Rank: 7Rank: 7Rank: 7
来 自:北京
等 级:黑侠
威 望:1
帖 子:113
专家分:522
注 册:2010-5-7
收藏
得分:12 
#include<stdio.h>
#include<malloc.h>
//#define NULL 0;//这里不需要定义NULL为0
struct student
{
    int age;
    char name[20];
    float score;
    struct student *next;
};
void main()
{
    int i;
    struct student *p,*head,*p1;
    for(i=0;i<3;i++)
    {
        if(i==0)head=p1=p=(struct student *)malloc(sizeof(struct student));
        else
        {
        p=(struct student *)malloc(sizeof(struct student));
        if(i==1)head->next=p1=p;
        if(i==2){p1->next=p;p->next=NULL;}
        }
    printf("please input the student information:\n");
    scanf("%20s%3d%5f",&p->name,&p->age,&p->score);
    p=head;
    }    //这里没;
   
   do
    {
        printf("%s's age is %d, score is%f\n",p->name,p->age,p->score);//美观~~
        p=p->next;
    }while(p!=NULL);
   
}
2010-05-17 09:55
gmtianxia
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-5-17
收藏
得分:0 
楼上的仙女太伟大了~   能解释一下 为什么 //#define NULL 0;//这里不需要定义NULL为0 吗?  课本里有些题目这样定义是为什么~?
2010-05-17 20:42
chichu
Rank: 2
来 自:安徽阜阳
等 级:论坛游民
帖 子:71
专家分:89
注 册:2010-4-14
收藏
得分:8 
回复 6楼 gmtianxia
呵呵,我也见到过那样的用法,首先将NULL置0就有些不规范,因为NLL本身是一个空指针一个常量,怎么能置0呢
但是呢,你在编缉运行是系统也不报错,呵呵
一本书上看到不知道观点对不对

有了目标才有动力!!!
2010-05-17 20:50
gmtianxia
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-5-17
收藏
得分:0 
以下是引用chichu在2010-5-17 20:50:33的发言:

呵呵,我也见到过那样的用法,首先将NULL置0就有些不规范,因为NLL本身是一个空指针一个常量,怎么能置0呢
但是呢,你在编缉运行是系统也不报错,呵呵
一本书上看到不知道观点对不对

谢谢! 这个论坛太强大了~ 高手多,重要的是热心的高手多!顶!
2010-05-17 22:24
快速回复:菜鸟求助,请各位大小虾帮忙看一下哪里出问题了~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044597 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved