我写了一个C程序
#include <stdio.h>
int *call()
{
int *a, b;
b = 43;
a = &b;
return a;
}
int main(void)
{
int *p;
p = call();
printf("%d\n", *p);
return 0;
}
我有这样的观察,我们不应该传递本地指针的地址或本地变量的地址。我用这个
编译了我的程序gcc -Wall -W 1.c -o 1
我没有看到任何警告。现在,我改变了我的程序
#include <stdio.h>
int *call()
{
int b;
b = 43;
return &b;//local variable's address
}
int main(void)
{
int *p;
p = call();
printf("%d\n", *p);
return 0;
}
并使用相同的gcc命令编译,
gcc -Wall -W 1.c -o 1
我收到警告
warning: function returns address of local variable
为什么在第一种情况下没有出现此警告?