返回指向局部变量的指针 [英] Returning pointer to local variable
本文介绍了返回指向局部变量的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
与返回指向局部变量的指针相关的问题是什么?
What is the problem associated with returning a pointer to a local variable?
并且(我不知道这样做是否合法)在main中返回对指针的引用有什么问题? 例如:
And (i don't know if its legal to do this) What is the problem with returning a reference to pointer in main? ex:
int *p;
p=abc();
abc将返回int&
where abc would be returning int&
P.S.对不起,如果不清楚.我也很困惑:P
P.S. Sorry if not clear. I am confused too :P
推荐答案
如果您的代码是:
int *abc() {
int n = 3;
return &n;
}
void foo() {
int m = 4;
}
int main() {
int *p;
p=abc();
foo();
printf("%d", *p);
}
然后m可能会覆盖n,结果将为4.
then m will probably overwrite n and the result will be 4.
这篇关于返回指向局部变量的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文