【发布时间】:2012-11-23 13:32:28
【问题描述】:
谁能帮我解释一下下面的代码:
为什么char *s 没有收到在foo() 分配的内存的点位置?
#include <stdio.h>
#include <stdlib.h>
char *foo()
{
char *s = (char *)malloc(20);
s = "Hello Heap.";
return s;
}
void bar(char *s)
{
s = foo();
printf("bar: %s\n", s); // Works fine just as expected.
}
int main()
{
char *s;
bar(s);
printf("%s\n", s); // Output some undefined content like `H?}?H??`, other than `Hello Heap.`
}
【问题讨论】:
标签: c malloc heap-memory