我有一个如下所示的函数
int* foo() {
int temp;
return(&temp); //address of temp variable.
编写与
相同的函数有什么问题int foo() {
int temp;
return(&temp); //address of temp variable.
}
因为“&”运算符返回内存位置的地址,它是一个整数。
最佳答案
编译器不遵循数字逻辑。对于它来说,&temp
是一个指针,而temp
是一个整数,并且是不同的类型,尽管两者都是下面的数字流。
只有强制转换才能让编译器相信您的逻辑。
类似
return (int)&temp
关于c - 函数返回类型作为指针为什么不作为 int ,同时返回变量的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10206559/