[HELP] 如何才可以输出"test"

wuzd 2011-01-06 02:50:49
这段代码有什么问题,如何才可以输出"test"?

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");
}
...全文
196 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuzd 2011-01-06
  • 打赏
  • 举报
回复
大家很热心,void GetDateString(char *&tmp)
就可以的.
mnbqwe1 2011-01-06
  • 打赏
  • 举报
回复
哥们 要自己独立思考 不能只是要答案
你那个程序只是传了拷贝 当然出了子函数就OVER了
用引用就可以了
muyunfengyd 2011-01-06
  • 打赏
  • 举报
回复
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");
}
  • 打赏
  • 举报
回复
有,可以跑的:

#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;
}
mnbqwe1 2011-01-06
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include "iostream"
using namespace std;

void a(int& s)
{
s=2;
}
int _tmain(int argc, _TCHAR* argv[])
{
int c=0;
int* b=&c;

a(*b);
cout<<*b<<endl;
return 0;
}
可以参考一下
mnbqwe1 2011-01-06
  • 打赏
  • 举报
回复
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");
}
go_Michael 2011-01-06
  • 打赏
  • 举报
回复
有完整点的代码吗
wuzd 2011-01-06
  • 打赏
  • 举报
回复
太谢谢大家,我不熟悉的太多了.

试了 void GetDateString(char *&tmp)

报的错是
Linking...
LogFile.obj : error LNK2019: unresolved external symbol "public: void __thiscall CLogFile::GetDateString(char *)" (?GetDateString@CLogFile@@QAEXPAD@Z) referenced in function "public: bool __thiscall CLogFile::OpenFile(void)" (?OpenFile@CLogFile@@QAE_NXZ)
大家多帮助.
czl8418 2011-01-06
  • 打赏
  • 举报
回复
把tmp=(char *)malloc(sizeof(char)*64)
放在函数外去NEW才可以,不然分配不到空间
iambic 2011-01-06
  • 打赏
  • 举报
回复
这几行代码问题百出,C++用户咋就不能学会用std::string呢。
zhaolinger2 2011-01-06
  • 打赏
  • 举报
回复
void GetDateString(char* &tmp)
龙哥依旧 2011-01-06
  • 打赏
  • 举报
回复
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");
}
go_Michael 2011-01-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 wuzd 的回复:]
这段代码有什么问题,如何才可以输出"test"?

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……
[/Quote]
你传入的tmp在函数中会产生一个副本,也就是说函数中你对tmp的操作只是对副本的操作,并没有影响实参
可以改为传入指针的引用
void GetDateString(char *&tmp)
copylegend 2011-01-06
  • 打赏
  • 举报
回复
你函数的参数不能直接传指针的值,。。。改成指针的指针吧。。。

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧