下列C/C++代码片段中哪段是可以正确执行的?()
注:如果你能在错误的代码上勾画和标注出错误的语句和原因,有额外加分。
A) voidGetMemory(char *p) {
p = (char*)malloc(100);
}
voidTest(void) {
char *str = NULL;
GetMemory(str);
strcpy(str,"hello world");
printf(“%s”, str);
}
char *GetMemory(void) {
char p[] ="hello world";
return p;
}
voidTest(void) {
char *str = NULL;
str = GetMemory();
printf(“%s”, str);
}
voidGetMemory(char **p, int num) {
*p = (char*)malloc(num);
}
void Test(void){
char*str = NULL;
GetMemory(&str,100);
strcpy(str,"hello");
printf(“%s”,str);
free(str);
}
void Test(void) {
char *str = (char *)malloc(100);
strcpy(str, “hello”);
free(str);
if(str != NULL) {
strcpy(str, “world”);
printf(“%s”,str);
}
}
这道题你会答吗?花几分钟告诉大家答案吧!