c - 我可以使用整数变量来定义数组长度吗?
问题描述
我可以使用变量来定义数组的大小吗?
int test = 12;
int testarr[test];
这行得通吗,我不想在初始化后更改数组的大小。'的int test
值在编译时是未知的。
解决方案
从 C99 开始,它是允许的,但仅适用于自动变量。
这是非法的:
int test = 12;
int testarr[test]; // illegal - static storage variable
int foo(void)
{
int test = 12;
static int testarr[test]; // illegal - static storage variable
}
唯一有效的形式是:
int foo(void)
{
int test = 12;
int testarr[test]; // legal - automatic storage variable
}
推荐阅读
- powerbi - Power BI 计算度量中某个值的出现次数
- c# - WPF Stylus 不触发事件,而 Touch 确实触发了手写笔事件
- node.js - 当要返回的列表取决于用户的权限时,REST URI 和 GET 请求的缓存
- reporting-services - SSRS Report 只返回单行输出而不是多页输出
- jenkins-pipeline - Jenkins 管道中的 Groovy 脚本 - java.lang.ClassCastException:
- swift - 如何使可点击成为 UIView 的一部分
- python - MongoDB Atlas中插入数据的问题
- c# - 如何使 FastNoise Lite 正常工作?
- javascript - 如何修复:TypeError:null 不是对象(评估“AgoraRtcChannelModule.prefix”)?
- ruby-on-rails - 在将 pg 列从文本迁移到二进制 (bytea) 的 Rails 迁移中,如何解决“bytea 类型的无效输入序列”?