我在数据结构的上机时碰到一个这样的问题;
即我先在函数前声明一个空指针,然后在函数中把这个空指针用new来赋值,然后返回函数,可是一出了函数体外那么这个已赋值的指针仍然变为空指针。我在各种语法书上都没有找到一个合理的解释,忘各位高手指点一二,在下不胜感激。
下面是测试用的具体代码:
// TestPointer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
class A
{
public:
int xx;
};
void testPointer(A *p_a);
int main(int argc, char* argv[])
{
A *a = NULL;
testPointer(a);
if(a == NULL)
{
printf("a keep on NULL and why?/n");
}
else
{
printf("a changed not NULL/n");
}
return 0;
}
void testPointer(A *p_a)
{
if(p_a == NULL)
{
p_a = new A;
}
p_a->xx = 100;
}