注册 登录
编程论坛 C++教室

各位大侠帮忙看看代码!!万分感谢!!!

phm0403 发布于 2008-08-16 09:41, 451 次点击
#include<iostream>
#include<string>
#include<malloc.h>
void GetMemory(char*p)
{
   p = (char*) malloc(100);
}
int main()
{      
     using namespace std;
  char *str = NULL ;
  GetMemory(str);
  strcpy(str,"Thunder");
  strcat(str+2,"Downloader");
  cout<<str<<endl;    
      return 0;

}
  这个代码是不是会崩溃??解释解释啊!!
1 回复
#2
xxp272008-08-16 11:32
p应该是一个引用

#include "stdafx.h"
#include<iostream>
#include<string>
#include<malloc.h>
void GetMemory(char*&p) //注意这里
{
   p = (char*) malloc(100);
}
int main()
{      
     using namespace std;
  char *str =NULL;
  GetMemory(str);
  strcpy(str,"Thunder");
  strcat(str+2,"Downloader");
  cout<<str<<endl;   
      return 0;

}
1