一个简单的链表复制,为什么不能通过地址传递将链表传递回来?解决办法
一个简单的链表复制,为什么不能通过地址传递将链表传递回来?
struct comb
{
int j;
struct comb *next;
}comb;
typedef struct comb *comblink;
void comblinkcopy(comblink dest, comblink sour)
{
struct comb *cbptr1,*cbptr2,*cbptr;
cbptr=sour;
cbptr1=(struct comb *)malloc(sizeof(struct comb));
assert(cbptr1!=NULL);
cbptr1-> j=cbptr-> j;
cbptr1-> next=NULL;
dest=cbptr1;
cbptr=cbptr-> next;
while(cbptr)
{
cbptr2=(struct comb *)malloc(sizeof(struct comb));
assert(cbptr2!=NULL);
cbptr2-> j=cbptr-> j;
cbptr1-> next=cbptr2;
cbptr2-> next=NULL;
cbptr1=cbptr2;
cbptr=cbptr-> next;
}
}
int main()
{
int i;
struct comb *cbptr1,*cbptr2;
comblink cbhead1,cbhead2,cbptr;
cbptr1=(struct comb *)malloc(sizeof(struct comb));
assert(cbptr1!=NULL);
cbptr1-> j=0;
cbptr1-> comblist=NULL;
cbptr1-> spevalink=NULL;
cbptr1-> next=NULL;
cbhead1=cbptr1;
for(i=0; i <4; i++)
{
cbptr2=(struct comb *)malloc(sizeof(struct comb));
assert(cbptr2!=NULL);
cbptr2-> j=i+1;
cbptr2-> comblist=NULL;
cbptr2-> spevalink=NULL;
cbptr2-> next=NULL;
cbptr1-> next=cbptr2;
cbptr1=cbptr2;
}
cbhead2=NULL;
comblinkcopy(cbhead2,cbhead1); /* 调用次函数后,为什么不能通过地址传递,传回在函数中所复制的链表 */
cbptr=cbhead2;
printf( "Output cbhead2.\n ");
while(cbptr)
{
printf( "j=%d\n ",cbptr-> j);
cbptr=cbptr-> next;
}
freecomblink(cbhead1);
freecomblink(cbhead2);
system( "PAUSE ");
return 0;
}
通过地址传递,不能将comblinkcopy函数中的dest链表返回给cbhead2么?
------解决方案--------------------
void comblinkcopy(comblink dest, comblink sour)
{
struct comb *cbptr1,*cbptr2,*cbptr;
cbptr=sour;
cbptr1=(struct comb *)malloc(sizeof(struct comb));
assert(cbptr1!=NULL);
cbptr1-> j=cbptr-> j;
cbptr1-> next=NULL;
dest=cbptr1;
cbptr=cbptr-> next;
while(cbptr)
{
cbptr2=(struct comb *)malloc(sizeof(struct comb));
assert(cbptr2!=NULL);
cbptr2-> j=cbptr-> j;
cbptr1-> next=cbptr2;
cbptr2-> next=NULL;
cbptr1=cbptr2;
cbptr=cbptr-> next;
}
}
其中对dest的修改是无效的,如果想修改dest本身,那么需要将其指针传递到函数中来,否则只能修改dest指向的内容,而不是dest本身
------解决方案--------------------
你修改的是副本, 需要影响外部程序的话,要么把指针return回去,要么修改指针的指针
------解决方案--------------------
指针本身是栈上的
------解决方案--------------------
comblinkcopy(cbhead2,cbhead1); /* 调用次函数后,为什么不能通过地址传递,传回在函数中所复制的链表 */
这样你没有达到地址传值的功能呀!
改成comblinkcopy(&cbhead2,&cbhead1);//注意不管那两个变量是指针与否都要用取地址符号那样能成
void comblinkcopy(comblink *dest, comblink *sour)//这函数部份也改成指向指针的,如果那两个变量是指针类型,那是这样!
------解决方案--------------------
comblinkcopy(cbhead2,cbhead1); /* 调用次函数后,为什么不能通过地址传递,传回在函数中所复制的链表 */
这个问题问的太多了,就是关于指针本身的副本问题.
struct comb
{
int j;
struct comb *next;
}comb;
typedef struct comb *comblink;
void comblinkcopy(comblink dest, comblink sour)
{
struct comb *cbptr1,*cbptr2,*cbptr;
cbptr=sour;
cbptr1=(struct comb *)malloc(sizeof(struct comb));
assert(cbptr1!=NULL);
cbptr1-> j=cbptr-> j;
cbptr1-> next=NULL;
dest=cbptr1;
cbptr=cbptr-> next;
while(cbptr)
{
cbptr2=(struct comb *)malloc(sizeof(struct comb));
assert(cbptr2!=NULL);
cbptr2-> j=cbptr-> j;
cbptr1-> next=cbptr2;
cbptr2-> next=NULL;
cbptr1=cbptr2;
cbptr=cbptr-> next;
}
}
int main()
{
int i;
struct comb *cbptr1,*cbptr2;
comblink cbhead1,cbhead2,cbptr;
cbptr1=(struct comb *)malloc(sizeof(struct comb));
assert(cbptr1!=NULL);
cbptr1-> j=0;
cbptr1-> comblist=NULL;
cbptr1-> spevalink=NULL;
cbptr1-> next=NULL;
cbhead1=cbptr1;
for(i=0; i <4; i++)
{
cbptr2=(struct comb *)malloc(sizeof(struct comb));
assert(cbptr2!=NULL);
cbptr2-> j=i+1;
cbptr2-> comblist=NULL;
cbptr2-> spevalink=NULL;
cbptr2-> next=NULL;
cbptr1-> next=cbptr2;
cbptr1=cbptr2;
}
cbhead2=NULL;
comblinkcopy(cbhead2,cbhead1); /* 调用次函数后,为什么不能通过地址传递,传回在函数中所复制的链表 */
cbptr=cbhead2;
printf( "Output cbhead2.\n ");
while(cbptr)
{
printf( "j=%d\n ",cbptr-> j);
cbptr=cbptr-> next;
}
freecomblink(cbhead1);
freecomblink(cbhead2);
system( "PAUSE ");
return 0;
}
通过地址传递,不能将comblinkcopy函数中的dest链表返回给cbhead2么?
------解决方案--------------------
void comblinkcopy(comblink dest, comblink sour)
{
struct comb *cbptr1,*cbptr2,*cbptr;
cbptr=sour;
cbptr1=(struct comb *)malloc(sizeof(struct comb));
assert(cbptr1!=NULL);
cbptr1-> j=cbptr-> j;
cbptr1-> next=NULL;
dest=cbptr1;
cbptr=cbptr-> next;
while(cbptr)
{
cbptr2=(struct comb *)malloc(sizeof(struct comb));
assert(cbptr2!=NULL);
cbptr2-> j=cbptr-> j;
cbptr1-> next=cbptr2;
cbptr2-> next=NULL;
cbptr1=cbptr2;
cbptr=cbptr-> next;
}
}
其中对dest的修改是无效的,如果想修改dest本身,那么需要将其指针传递到函数中来,否则只能修改dest指向的内容,而不是dest本身
------解决方案--------------------
你修改的是副本, 需要影响外部程序的话,要么把指针return回去,要么修改指针的指针
------解决方案--------------------
指针本身是栈上的
------解决方案--------------------
comblinkcopy(cbhead2,cbhead1); /* 调用次函数后,为什么不能通过地址传递,传回在函数中所复制的链表 */
这样你没有达到地址传值的功能呀!
改成comblinkcopy(&cbhead2,&cbhead1);//注意不管那两个变量是指针与否都要用取地址符号那样能成
void comblinkcopy(comblink *dest, comblink *sour)//这函数部份也改成指向指针的,如果那两个变量是指针类型,那是这样!
------解决方案--------------------
comblinkcopy(cbhead2,cbhead1); /* 调用次函数后,为什么不能通过地址传递,传回在函数中所复制的链表 */
这个问题问的太多了,就是关于指针本身的副本问题.