我正在使用 Atmel 6.2 并为 Arduino 编写应用程序。我对这些代码行有疑问:
int total = 3;
uint64_t myarray[total] = {};
出现如下错误
error: array bound is not an integer constant before ']' token
为什么会这样?
最佳答案
这个
int total = 3;
uint64_t myarray[total] = {};
是可变大小数组的定义,因为数组的大小不是编译时常量表达式。
C99 有条件地支持这种数组。然而,此功能在 C++ 中不存在(尽管某些编译器可以有自己的语言扩展,在 C++ 中包含此功能)并且编译器会正确发出错误。
要么你应该在数组的定义中使用一个常量,例如像这样
const int total = 3;
uint64_t myarray[total] = {};
或者您应该考虑使用另一个容器,例如 std::vector<uint64_t>
如果您认为数组的大小可以在运行时更改。
关于c++ - 如何在 C++ 中将 uint64_t 数组初始化为 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31849915/