怎么回事啊,难道程序错了?
#include"stdio.h"#include"iostream.h"
char *getmemory(void)
{
char p[]="hello word";
return p;
}
void main()
{
char *str=NULL;
str=getmemory();
printf(str);
getchar();
}
结果是:hell@ .为什么不是:hello word
程序代码:#include"iostream.h"
#include<stdio.h>
#include<string.h>
char* getmemory(void)
{
char*p;
p=new char[50];
strcpy(p,"hello,world!");
return p;
}
void main()
{
char *str=NULL;
str=getmemory();
printf(str);
getchar();
delete[] str; //这句不能少,否则容易发生内存泄露!
}