I typed this code:
void main()
{
int *a;
a = foo();
printf("%d\n", *a);
}
int* foo()
{
int b = 10;
return &b;
}
编译后,出现了2个问题:
1. error - Conflicting type for foo()
2. warning - function returns address of local variable
但是当我写下这句话时,
它看起来就像这样
。int* foo();
void main()
{
int *a;
a = foo();
printf("%d\n", *a);
}
int* foo()
{
int b = 10;
return &b;
}
现在编译后不会出现错误,这很明显。但是,为什么编译器没有对返回本地变量地址的函数发出警告呢?
声明或不声明函数如何影响返回本地变量地址?
抱歉之前没有提到,我正在使用GNU GCC编译器。
warning: address of stack memory associated with local variable 'b' returned [-Wreturn-stack-address]
警告。 - mauro