标题:分析这个问题?
取消只看楼主
jinzhong620
Rank: 1
等 级:新手上路
帖 子:91
专家分:0
注 册:2007-5-28
 问题点数:0 回复次数:3 
分析这个问题?

void GetMemory(char *p)

{

p = (char *)malloc(100);

}

void Test(void)

{

char *str = NULL;

GetMemory(str);

strcpy(str, "hello world");

printf(str);

}

请问运行Test函数会有什么样的结果?

答:

char *GetMemory(void)

{

char p[] = "hello world";

return p;

}

void Test(void)

{

char *str = NULL;

str = GetMemory();

printf(str);

}

请问运行Test函数会有什么样的结果?

答:

Void GetMemory2(char **p, int num)

{

*p = (char *)malloc(num);

}

void Test(void)

{

char *str = NULL;

GetMemory(&str, 100);

strcpy(str, "hello");

printf(str);

}

请问运行Test函数会有什么样的结果?

答:

void Test(void)

{

char *str = (char *) malloc(100);

strcpy(str, “hello”);

free(str);

if(str != NULL)

{

strcpy(str, “world”);

printf(str);

}

}

请问运行Test函数会有什么样的结果?

答:

2007-06-22 19:51
jinzhong620
Rank: 1
等 级:新手上路
帖 子:91
专家分:0
注 册:2007-5-28
得分:0 

void GetMemory(char *p)

{

p = (char *)malloc(100);

}

void Test(void)

{

char *str = NULL;

GetMemory(str);

strcpy(str, "hello world");

printf(str);

char *GetMemory(void)

{

char p[] = "hello world";

return p;

}

void Test(void)

{

char *str = NULL;

str = GetMemory();

printf(str);

}

Void GetMemory2(char **p, int num)

{

*p = (char *)malloc(num);

}

void Test(void)

{

char *str = NULL;

GetMemory(&str, 100);

strcpy(str, "hello");

printf(str);

}

void Test(void)

{

char *str = (char *) malloc(100);

strcpy(str, “hello”);

free(str);

if(str != NULL)

{

strcpy(str, “world”);

printf(str);

}

}

基本就是这样?

珍惜我们现在所爱的人,不要在一万年以后才说我爱你!
2007-06-22 21:04
jinzhong620
Rank: 1
等 级:新手上路
帖 子:91
专家分:0
注 册:2007-5-28
得分:0 
就是了

珍惜我们现在所爱的人,不要在一万年以后才说我爱你!
2007-06-23 07:59
jinzhong620
Rank: 1
等 级:新手上路
帖 子:91
专家分:0
注 册:2007-5-28
得分:0 
怎么了,不好吗?这是我的一个同学给的。

珍惜我们现在所爱的人,不要在一万年以后才说我爱你!
2007-06-24 09:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-149588-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.101589 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved