使用(int)sqrt(N)导致'数组绑定不是整数常量'

Sco*_*tie 1 c

#include <stdio.h>
#include <math.h>

int size = (int) sqrt(4);
int arr[size];

int main() {
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到了:

test.c:5: error: array bound is not an integer constant
Run Code Online (Sandbox Code Playgroud)

有人能帮助我吗?

cni*_*tar 5

您无法使用可变大小的静态存储(例如"全局"数组)定义数组.如果阵列具有自动存储(如果它是函数中的数组),它将作为VLA工作.

正如icepack正确指出的那样,在C99中正式引入了VLA.

  • 应该注意 - 只在C99 (2认同)