| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 460 人关注过本帖
标题:链表?????
只看楼主 加入收藏
xufan123
Rank: 5Rank: 5
等 级:职业侠客
帖 子:226
专家分:318
注 册:2010-11-15
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
链表?????
#define NULL 0
struct student
{
long num;
char name[20];
float score[3];
struct student *next;
};
void main()
{
struct student *p,*head;
struct student a,b,c;
a.num=10036;a.name="xf";
b.num=10037;b.name="jbm";
c.num=10038;c.name="zz";
head=&a;
a.next=&b;
b.next=&c;
c.next=NULL;
p=head;
do
{
printf("%-5d%-6s",p->num,p->name);
p=p->next;
}
while(p!=NULL);
}

这样做也错????
搜索更多相关主题的帖子: 链表 
2010-12-16 15:20
fy7518716
Rank: 2
等 级:论坛游民
帖 子:8
专家分:24
注 册:2008-6-17
收藏
得分:7 
a.name="xf";这样在c语言中是不对的。字符串处理是c语言的弱项,用起来比较麻烦。
改成这样  strcpy(a.name, "xf");   加上头文件 #include <string.h>
另外 #define NULL 0    是多余的,有可能产生冲突
2010-12-16 15:52
广陵绝唱
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:29
帖 子:3607
专家分:1709
注 册:2008-2-15
收藏
得分:7 
程序代码:
#include<stdio.h>
#include<string.h>
#define NULL 0
struct student
{
    long num;
    char name[20];
    float score[3];
    struct student *next;
};
int main(void)
{
    struct student *p,*head;
    struct student a,b,c;

    a.num=10036;strcpy(a.name,"xf");
    b.num=10037;strcpy(b.name,"jbm");
    c.num=10038;strcpy(c.name,"zz");
    head=&a;
    a.next=&b;
    b.next=&c;
    c.next=NULL;
    p=head;
    do
    {
        printf("%-5d%-6s",p->num,p->name);
        p=p->next;
    }
    while(p!=NULL);

    return 0;
}

2010-12-16 16:52
hdzhanjingyu
Rank: 2
等 级:论坛游民
帖 子:26
专家分:29
注 册:2010-12-3
收藏
得分:7 
    #include<stdio.h>
#define NULL 0
struct student
{
long num;
char name[20];
float score[3];
struct student *next;
};
void main()
{
struct student *p,*head;
struct student a={10036,"xf"},b={10037,"jbm"},c={10038,"zz"};
//a.num=10036;a.name="xf";不能给字符数组整体赋值,这样写是错的,也可以用strcpy(a.name, "xf");  加上头文件 #include <string.h>
//b.num=10037;b.name="jbm";
//c.num=10038;c.name="zz";

head=&a;
a.next=&b;
b.next=&c;
c.next=NULL;
p=head;
do
{
printf("%-5d%-6s",p->num,p->name);
p=p->next;
}
while(p!=NULL);
}
2010-12-16 17:06
xufan123
Rank: 5Rank: 5
等 级:职业侠客
帖 子:226
专家分:318
注 册:2010-11-15
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
这个怎么这样啊 ???我用了strcpy 还加了文件包含!
2010-12-16 17:58
快速回复:链表?????
数据加载中...
 
   



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

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