gpt4 book ai didi

c++ - 在C++书籍中,array bound必须是常量表达式,但为什么下面的代码有效?

转载 作者:可可西里 更新时间:2023-11-01 16:36:35 26 4
gpt4 key购买 nike

#include <iostream>
using namespace std;

int main(){
int n=10;
int a[n];

for (int i=0; i<n; i++) {
a[i]=i+1;
cout<<a[i]<<endl;
}
return 0;
}

在 Mac 下的 Xcode4 中运行良好

按照书上的说法,应该是错的,为什么?

好迷茫~

最佳答案

这是一个名为 VLA 的 C99 特性一些编译器也允许在 C++ 中使用。它是在堆栈上分配的,就像 int a[10] 一样。

关于c++ - 在C++书籍中,array bound必须是常量表达式,但为什么下面的代码有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5947661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com