Variably modified array at file scope
来源:互联网 发布:windows怎么更新系统 编辑:程序博客网 时间:2024/06/07 19:11
C语言编译错误:Variably modified array at file scope
转载自此
今天在编译一段C源程序时,遇到编译错误提示:Variably modified array at file scope。原因在于代码头部有这样几行:
12
const int length = 256;int a[length];
在C语言中,const不是一个真真正正的常量,其代表的含义仅仅是只读。使用const声明的对象是一个运行时对象,无法使用其作为某个量的初值、数组的长度、case的值或在类型的情形中使用。以上是全局的情况,那么仅在一个块中定义的情况呢?下例中const所限定的值超出其生命周期后可被修改这很容易理解不必多作解释。
12345
int i;for(i = 0; i < 6; ++i) {const int j = i; // 试试 const int j = rand();printf("%d", j); // Output: 012345}
如果要在C中定义编译时常量,也就是一个真正的常量,可以使用#define宏定义,但是#define的问题在于它是直接替换源代码中的所有匹配字符串,容易造成误替换,因此对于像int型这样的情况可以耍点小把戏,就像这样:
12
enum {length = 256}; // Or: #define length 256int a[length];
那么我们经常说const也并非不可修改,利用某些小技巧可以绕过只读约束,下面的例子中被const限定的t就仍然可被修改:
123
const int t = 1;*(int*)&t = 0;printf("%d",t); // Output: 0
参考资料
ISO Sec. 6.4; H&S Secs. 7.11.2,7.11.3 pp. 226-7
阅读全文
0 0
- Variably modified array at file scope
- Variably modified array at file scope
- Variably modified array at file scope
- Variably modified array at file scope
- Variably modified array at file scope
- error: variably modified 'table' at file scope
- const——error: variably modified ‘buf’ at file scope
- variably modified 'a' at file scope 之减治法---假币问题
- 错误信息 error: variably modified 'buf' at file scope char buf[bufsize];
- C : stack memory, goto and “jump into scope of identifier with variably modified type”,
- swift:Declaration is only valid at file scope问题解决
- error C2449: found '{' at file scope (missing function header?)和error C2059: syntax error : '}'
- 代码管理工具SourceTree之Modified Binary File
- Free Heap block ***modified at ***after it was freed
- Heap block at 001547A0 modified at 001547DC past requested size of 34
- Heap block at 0075E6C8 modified at 0075E736 past requested size of 66
- Apple LLVM compiler 3.0 error file".h" has been modified.....
- file ' ' has been modified since the precompiled header ' ' was built
- opencv3.0配置
- 并发用户数与TPS之间的关系
- 你知道 Google X 神秘实验室创始人的故事么?
- 欢迎使用CSDN-markdown编辑器
- 微信小程序循环列表添加默认事件,默认样式,点击后修改样式
- Variably modified array at file scope
- 图片懒加载插件
- 什么是ES6
- input file类型,文件类型的限制
- Nginx详细安装部署教程
- 如何将免安装版的TOMCAT注册成系统服务
- win7有关安装AppServ的问题
- l2fwd 测试
- Leetcode:513. Find Bottom Left Tree Value