64,282
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <iostream>
#include <string.h>
using namespace std;
void GetDataString(char* &tmp)
{
char* s="test";
int len = strlen(s)+1;//多一个字节用于存放\0结束符
tmp = (char*)malloc(len);//我使用多字节
strcpy(tmp,s);
tmp[len-1]='\0';//最后一个字节加上字符串结束符号
}
int _tmain(int argc, _TCHAR* argv[])
{
char* str = NULL;
GetDataString(str);
if(str)
{
cout << str << endl;
free(str);
}
return 0;
}
char *tmp2;
GetDateString(tmp2);
cout<<tmp2;
free(tmp2);
void GetDateString(char *&tmp){
time_t t = time(0);
tmp=(char *)malloc(sizeof(char)*64);
strcpy(tmp,"test");
}
void GetDateString2(char **tmp){
time_t t = time(0);
*tmp=(char *)malloc(sizeof(char)*64);
strcpy(*tmp,"test");
}