Developer Network
Developer Network
Developer
:CreateViewProfileText:
登录
MSDN 订阅
获取工具
下载
Visual Studio
SDK
试用软件
免费下载
Office 资源
计划
订阅
Administrators
学生
Microsoft Imagine
Microsoft 学生合作伙伴
ISV
新手
Events(事件)
社区
Magazine
论坛
博客
第 9 频道
文档
API 和参考
开发人员中心
示例
停用的内容
非常抱歉。你请求的内容已被删除。将在 1 秒内自动重定向。
提出问题
快速访问
论坛主页
浏览论坛用户
FAQ
搜索相关主题
Remove From My Forums
积极答复者
这段代码出错的原因是什么?
Visual Studio Development
>
Visual C++
问题
0
登录进行投票
void GetMemory(char *p) {
//p = (char *)malloc(100);
p = new char[100];
} void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
int _tmain(int argc, _TCHAR* argv[])
{
Test();
}
something is OK.
2009年8月12日 8:49
回复
|
引用
答案
0
登录进行投票
void GetMemory(char **p)
{
//p = (char *)malloc(100);
*p = new char[100];
}
void Test(void)
{
char *str = NULL;
GetMemory(&str);
strcpy(str, "hello world");
printf(str);
}
int _tmain(int argc, _TCHAR* argv[])
{
Test();
}
已标记为答案
Tim Li
2009年8月20日 9:17
2009年8月12日 9:01
回复
|
引用
0
登录进行投票
因为char *str指针在参数传递时考贝了一份
因此传递过去的将不是str
可以这样
void GetMemory(char*& p)
{
//p = (char *)malloc(100);
p = new char[100];
}
就明白了
已标记为答案
Tim Li
2009年8月20日 9:17
2009年8月12日 9:10
回复
|
引用
0
登录进行投票
你要想返回指针必须要传2重指针.
void GetMemory(char **p)
{
*p = new char[100];
}
void Test(void)
{
char *str = NULL;
GetMemory(&str);
strcpy(str, "hello world");
printf(str);
//必须要释放.
delete str;
}
这样才能返回指针.
0xBAADF00D
已标记为答案
Tim Li
2009年8月20日 9:17
2009年8月12日 10:01
回复
|
引用
版主
全部回复
0
登录进行投票
void GetMemory(char **p)
{
//p = (char *)malloc(100);
*p = new char[100];
}
void Test(void)
{
char *str = NULL;
GetMemory(&str);
strcpy(str, "hello world");
printf(str);
}
int _tmain(int argc, _TCHAR* argv[])
{
Test();
}
已标记为答案
Tim Li
2009年8月20日 9:17
2009年8月12日 9:01
回复
|
引用
0
登录进行投票
能解释下原因吗??
something is OK.
2009年8月12日 9:06
回复
|
引用
0
登录进行投票
因为char *str指针在参数传递时考贝了一份
因此传递过去的将不是str
可以这样
void GetMemory(char*& p)
{
//p = (char *)malloc(100);
p = new char[100];
}
就明白了
已标记为答案
Tim Li
2009年8月20日 9:17
2009年8月12日 9:10
回复
|
引用
0
登录进行投票
能详细点吗 呵呵
something is OK.
2009年8月12日 9:28
回复
|
引用
0
登录进行投票
你要想返回指针必须要传2重指针.
void GetMemory(char **p)
{
*p = new char[100];
}
void Test(void)
{
char *str = NULL;
GetMemory(&str);
strcpy(str, "hello world");
printf(str);
//必须要释放.
delete str;
}
这样才能返回指针.
0xBAADF00D
已标记为答案
Tim Li
2009年8月20日 9:17
2009年8月12日 10:01
回复
|
引用
版主
0
登录进行投票
实参和形参的问题,谭浩强的C语言就有讲得很清楚,楼主可以看一下
2009年8月18日 8:10
回复
|
引用