为什么GetMemory()函数返回的指针不对?
程序代码:#include "u_def.h"
#include <stdio.h>
#include "u_linearStruct.h"
char *GetMemory(void)
{
const char p[] = "hello world.\n";
return (char*)p;
}
int main( int ac, char **av )
{
char *pstr = NULL;
pstr = GetMemory();
printf("%s\n", pstr);
return 0;
}子函数char *GetMemory(void)中的变量p是const类型的,应该不是在堆栈中,但是为什么程序还是不能输出 hello world. 呢?在VC6编译器时 提 示:warning C4172: returning address of local variable or temporary,
而当我设置成 static char p[] = "hello world.\n";后就发现没有这个警告warning C4172: returning address of local variable or temporary了!
说明这个p还 是局部变量呀?看来这个const只能约束变量是不变的,而并不能说明是常量区吗?
[ 本帖最后由 vfdff 于 2010-7-17 03:11 编辑 ]









