use*_*783 3 c string malloc free char
我遇到了一家公司的两个问题.这两个问题让我困惑.任何人都可以帮助解释答案的原因吗?
写下结果.
void Test(void){
char *str = (char *) malloc(100);
strcpy(str, “hello”);
free(str);
if(str != NULL){
strcpy(str, “world”);
printf(str);
}
}
Run Code Online (Sandbox Code Playgroud)
答:它会输出"世界"
写下结果.
char *GetMemory(void){
char p[] = "hello world";
return p;
}
void Test(void){
char *str = NULL;
str = GetMemory();
printf(str);
}
Run Code Online (Sandbox Code Playgroud)
Ans:输出未知,因为指针无效.