c - 数组大小在运行时确定

标签 c arrays gcc variable-length-array

我看到一些这样的代码:

int foo()
{
  int sz = call_other_func();
  char array[sz];

  /* whatever */
}

我对它如何工作甚至用 gcc 编译感到困惑。数组的大小应该是静态的 并在编译时确定,不是吗?

最佳答案

这种类型的数组称为variable length arrays (您还想阅读:Arrays of Variable Length - GCC)并且只允许在 C99 中使用。通过使用 VLA,可以在运行时确定数组的大小。

关于c - 数组大小在运行时确定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20096253/

相关文章:

c++ - 设计模式 : C++ Abstraction Layer

php - 通知: Array to string conversion in error when requesting data from a databse

visual-studio - -isystem 用于 MS Visual Studio C++ 编译器

c - 使用 gcc 获取二进制文件中不带路径的文件名

javascript - 防止 css 应用于数组中的第一个渲染对象

c++ - 错误 "undefined reference to omp_get_wtime"

c - 如何在不使用库的情况下获取 jpeg 文件的宽度/高度?

c - 在 c 中从字符串中解析数字比 strtok 更好的选择?

c - 如何按名称(字符串)搜索和排序 BST?按队列打印并缩进?

java - 在 Android Studio 中从 JSON 中的数组中获取字符串