请各位看这段代码,为什么同一指针,没有另外赋值结果却不一样呢?

popo00fa 2008-09-16 09:04:21
源代码如下:问题见标记处
typedef struct Node
{
int data;
int ID;
struct Node* next;
}*PNode;

typedef struct LinkList
{
PNode head;
int len;
}*PLink;

PLink CreateLinkList(int L)
{
PLink p_L=new LinkList;
p_L->head=new Node;
cout<<"Get p_L->head Address in Create***"<<p_L->head<<endl; //这里获得头结点的地址。

if(!p_L)
cout<<"Apply Memory for LinkList fail\n";
if(!p_L->head)
cout<<"Apply Memory for Node fail\n";

p_L->head->ID=0;
p_L->len=L;
p_L->head->next=NULL;

PNode p;
if(L>0)
{
while(L)
{
p=new Node;
cin>>p->data;
p->ID=L;
p->next=p_L->head->next;
p_L->head->next=p;
--L;
}
}
else
{
p=NULL;
p_L->head->next=p;
}
return (p_L);
}

int GetElem(PLink pl)
{
cout<<"Get pl->head Address in Get***"<<pl->head<<endl;//这里获得的头结点地址与前面获得不一样,差一个Node单元。
PNode p;
p=pl->head->next;
while(p)
{
cout<<p->ID<<"***"<<p->data<<endl;
p=p->next;
}
return 0;
}

各位高人麻烦了!就是这里弄不清,知识面太浅了!
...全文
89 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangdeqie 2008-09-16
  • 打赏
  • 举报
回复

//这地址不是一样吗?
//运行结果:
Get p_L->head Address in Create***00AA07E0
1
Get pl->head Address in Get***00AA07E0
1***1
Press any key to continue



#include <iostream>
using namespace std;

typedef struct Node
{
int data;
int ID;
struct Node* next;
}*PNode;

typedef struct LinkList
{
PNode head;
int len;
}*PLink;

PLink CreateLinkList(int L)
{
PLink p_L=new LinkList;
p_L->head=new Node;
cout <<"Get p_L->head Address in Create***" <<p_L->head <<endl; //这里获得头结点的地址。

if(!p_L)
cout <<"Apply Memory for LinkList fail\n";
if(!p_L->head)
cout <<"Apply Memory for Node fail\n";

p_L->head->ID=0;
p_L->len=L;
p_L->head->next=NULL;

PNode p;
if(L>0)
{
while(L)
{
p=new Node;
cin>>p->data;
p->ID=L;
p->next=p_L->head->next;
p_L->head->next=p;
--L;
}
}
else
{
p=NULL;
p_L->head->next=p;
}
return (p_L);
}

int GetElem(PLink pl)
{
if (!pl)
{
cout<<"NULL";
return 0;
}
cout <<"Get pl->head Address in Get***" <<pl->head <<endl;//这里获得的头结点地址与前面获得不一样,差一个Node单元。
PNode p;
p=pl->head->next;
while(p)
{
cout <<p->ID <<"***" <<p->data <<endl;
p=p->next;
}
return 0;
}

void main()
{
PLink p=CreateLinkList(1);
GetElem(p);
}
popo00fa 2008-09-16
  • 打赏
  • 举报
回复
是的!我刚意识到,其它地方调用过pl指针,估计是用了引用了那,可能那里修改了!不好意思!!麻烦了!!
哎!搞得我自己都郁闷了大半天!谢谢!!!
wuyu637 2008-09-16
  • 打赏
  • 举报
回复

typedef struct Node
{
int data;
int ID;
struct Node* next;
}*PNode;

typedef struct LinkList
{
PNode head;
int len;
}*PLink;

PLink CreateLinkList(int L)
{
PLink p_L=new LinkList;
p_L->head=new Node;
cout <<"Get p_L->head Address in Create***" <<p_L->head <<endl; //这里获得头结点的地址。

if(!p_L)
cout <<"Apply Memory for LinkList fail\n";
if(!p_L->head)
cout <<"Apply Memory for Node fail\n";

p_L->head->ID=0;
p_L->len=L;
p_L->head->next=NULL;

PNode p;
if(L>0)
{
while(L)
{
p=new Node;
cin>>p->data;
p->ID=L;
p->next=p_L->head->next;
p_L->head->next=p;
--L;
}
}
else
{
p=NULL;
p_L->head->next=p;
}
return (p_L);
}

int GetElem(PLink pl)
{
cout <<"Get pl->head Address in Get***" <<pl->head <<endl;//这里获得的头结点地址与前面获得不一样,差一个Node单元。
PNode p;
p=pl->head->next;
while(p)
{
cout <<p->ID <<"***" <<p->data <<endl;
p=p->next;
}
return 0;
}

void main()
{

PLink tPnode;
tPnode = CreateLinkList(5);
GetElem(tPnode);
cout << "hello world" << endl;
}

=========================================
Get p_L->head Address in Create***003A58F8
d
Get pl->head Address in Get***003A58F8
1***-842150451
2***-842150451
3***-842150451
4***-842150451
5***-842150451
hello world
请按任意键继续. . .






测试没有问题啊

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧