![]() |
#2
rjsp2021-12-19 10:47
|
本程序在vs2010中进行的调试。

#include<stdio.h>
//结构体定义
typedef struct LNode
{
int date;
struct LNode *next;
}node,*LinkNode;
//问题测试函数
void issue(LinkNode &L)
{
printf("\n进入函数内\nL的地址%x, L指向的地址%x, L->date的值%d和地址%x, L->next的值%x\n",&L,L,L->date,&L->date,L->next);
node p;
L=&p; //-----关键点,注释后,则第二次进入函数,L->date正常不会变了
L->date=2;
printf("赋值后\nL的地址%x, L指向的地址%x, L->date的值%d和地址%x, L->next的值%x\n\n",&L,L,L->date,&L->date,L->next);
}
//主函数
void main()
{
LinkNode L=new node;
printf("\n初始化后\nL的地址%x, L指向的地址%x, L->date的值%d和地址%x, L->next的值%x\n",&L,L,L->date,&L->date,L->next);
issue(L);
printf("\n主函数\nL的地址%x, L指向的地址%x, L->date的值%d和地址%x, L->next的值%x\n",&L,L,L->date,&L->date,L->next);
issue(L);
printf("\n主函数\nL的地址%x, L指向的地址%x, L->date的值%d和地址%x, L->next的值%x\n\n",&L,L,L->date,&L->date,L->next);
}
输出结果如下:
我上传不了图片,就描述一下吧:
在函数中,让结构体指针指向“在函数内创建的结构体(不是结构体指针哦)”并给date赋值,回到主函数后->date值正常。
但再传指针进去某个函数后,发现它的->date就变了。
通过输出地址,发现结构体指针&L正常,结构体指针指向的地址L也没有变化,就是结构体的成员L->date变了。
并且发现,“结构体指针指向的地址”与结构体成员“L-date”的地址一样。
