为什么一个函数不返回本地阵列? [英] Why should a function not return a local array?

查看:93
本文介绍了为什么一个函数不返回本地阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char* f()
{
char s[100];
//....function body code
return s;
}

为什么它不这样写?

Why should it not be written like this?

推荐答案

取值是一个局部变量,只有在函数内部存在。

s is a local variable that only exists within the function.

一旦函数退出,取值不再存在,它的内存将被重新分配给你的程序的其他部分。

Once the function exits, s no longer exists, and its memory will be re-allocated to other parts of your program.

因此​​,你的函数返回一个指向内存随机无意义块。

Therefore, your function is returning a pointer to a random meaningless block of memory.

这篇关于为什么一个函数不返回本地阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆