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

分析这个问题?

jinzhong620 发布于 2007-06-22 19:51, 407 次点击

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函数会有什么样的结果?

答:

8 回复
#2
aipb20072007-06-22 20:17
看了下头两个,一的个错误,因为指针应该按引用调用,二的个错误,因为返回的指针局部变量。


你能不能写紧凑点,看的费力!
#3
jinzhong6202007-06-22 21:04

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);

}

}

基本就是这样?
#4
野比2007-06-22 21:17
我帮你重排版吧.... 看得我好累...

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-6-22 21:18:57编辑过]

#5
jinzhong6202007-06-23 07:59
就是了
#6
I喜欢c2007-06-23 21:50
考我眼力和记忆力哇?  呵呵..
#7
herbert_19872007-06-23 21:56
以下是引用I喜欢c在2007-6-23 21:50:35的发言:
考我眼力和记忆力哇? 呵呵..

哈哈 说的也是!

#8
jinzhong6202007-06-24 09:44
怎么了,不好吗?这是我的一个同学给的。
#9
I喜欢c2007-06-24 09:56
不是说不好.. 格式..
1