鱼C论坛

 找回密码
 立即注册
查看: 4125|回复: 2

结构体链表的问题

[复制链接]
发表于 2013-5-21 11:25:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
下面代码编译器没报错,但是一运行怎么就崩溃了???
哪有问题?
#include <iostream>
using namespace std;
struct book
{
        int num;
        float price;
        struct book*next;
};
void main()
{
        book x,y,z,*head,*p;
        x.num=10000;
        x.price=14.5f;
        y.num=20000;
        y.price=23.4f;
        z.num=30000;
        z.price=45.6f;
        head=&x;
        x.next=&y;
        y.next=&z;   
        z.next=NULL;      
        while (p!=NULL)
        {
                cout<<p->num<<endl<<p->price<<endl;
                p=p->next;
        }

}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-5-21 11:36:55 | 显示全部楼层
很明显你p没有赋初值嘛,
像这种问题应该学会自己调试的,



#include <iostream>
using namespace std;
struct book
{
        int num;
        float price;
        struct book *next;
};
void main()
{
        book x,y,z,*head,*p;
        x.num=10000;
        x.price=14.5f;
        y.num=20000;
        y.price=23.4f;
        z.num=30000;
        z.price=45.6f;
        head=&x;
        x.next=&y;
        y.next=&z;   
        z.next=NULL;      
        p = head;
        while (p!=NULL)
        {
                cout<<p->num<<endl;
                                cout<<p->price<<endl;
                p=p->next;
        }

}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2013-5-26 10:26:02 | 显示全部楼层
强烈支持楼主ing……
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-28 02:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表