#include
#include
struct Process
{
char name[5];
int again;
int serve;
char state;
struct Process *next;
};
void Set(struct Process *L)
{
struct Process Demo;
L=&Demo;
printf("%p\n",L);
}
int main(void)
{
struct Process *L=NULL;
Set(L);
if(L)
{
printf("函数结束,空间释放!\n");
printf("%p\n",L);
}
else
printf("L地址为NULL\n");
return 0;
}
程序的运行结果为:
0000000000FDF0
L的地址为NULL
在这个程序中,我在main函数中声明了*L指针,本来打算在Set()函数中把L的地址改为Demo结构体变量的地址,可是程序的运行结果不对呀!怎么回事?求详解分析下!
只有1个币了,只能这次酬谢这么多了!谢谢呐!
你的set函数中demo变量本来就是空值,你把它赋给L它肯定输出空值啊
main函数中的L和set()函数中的L是不同变量,
set()函数中改变的形参L的指向,不改变main中的L指向。
按你的意图应该修改如下:
#include <stdio.h>
struct Process
{
char name[5];
int again;
int serve;
char state;
struct Process *next;
};
void Set(struct Process *&L) //改为引用
{
struct Process Demo;
L=&Demo;
printf("%p\n",L);
}
int main(void)
{
struct Process *L=NULL;
Set(L);
if(L)
{
printf("函数结束,空间释放!\n");
printf("%p\n",L);
}
else
printf("L地址为NULL\n");
return 0;
}
Set(&L);//改成这样的试试
你的问题应该是二级指针问题,你可以看看下面的博文,理解下,然后重新更改下你的结构体定义
http://blog.csdn.net/touch_2011/article/details/6989538
这是之后自己找到的一篇博客,里面讲的东西感觉挺好理解的。http://www.cnblogs.com/chinacloud/archive/2011/09/02/2163377.html