[英]C local variable address return warning
I have this function in C: 我在C中具有此功能:
static Node* newNode(void* e){
Node n={e,NULL,NULL};
return &n;
}
And while compiling I get the following warning that I would like to understand why it happens: 在编译时,我收到以下警告,我想了解为什么会发生:
warning: function returns address of local variable [enabled by default]
What kind of dangers are lurking behind this? 这背后隐藏着什么样的危险?
Thank you 谢谢
Local variables are destroyed when you return from a function. 从函数返回时,局部变量将被销毁。 Accessing them after the function returned is undefined behavior, don't do this. 返回函数后访问它们是未定义的行为,请不要这样做。
警告是因为变量的作用域是函数的局部变量-一旦返回函数,该变量就不再在作用域中,并且其值是不确定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.