我知道函数中的变量正在使用堆栈空间。当函数退出时,空间被释放。这就是为什么我们应该在函数中将指针变量声明为静态。但是,我发现下面的代码运行良好。
海湾合作委员会版本是: gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)
#include <stdio.h>
char *month_name(int n) {
char *name[] = {"Invalid name", "Jan.", "Feb", "Mar", "Apr", "May", "June",
"July", "Aug", "Sep", "Oct", "Nov", "Dec"};
return n < 1 || n > 12 ? name[0] : name[n];
}
int main() {
char *month;
month = month_name(2);
printf("%s\n", month); // The output is Feb
}
Run Code Online (Sandbox Code Playgroud)
看来函数中的变量被隐式转换为静态。谁能为我解释一下吗?提前致谢。
我克隆了 vscode 存储库并运行yarn以安装 deps。但出现错误:
[1/4] Resolving packages...
[2/4] Fetching packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/xterm-addon-unicode11/-/xterm-addon-unicode11-0.3.0.tgz: incorrect data check".
info If you think this is a bug, please open a bug report with the information provided in "/Users/lsf/Code/github/js/vscode/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
Run Code Online (Sandbox Code Playgroud)
更新:
操作系统:MacOS Big Sur - 11.5.2(Apple M1 芯片)
节点版本:v14.18.1
纱线版本:10.22.10