c - 声明为 const,但定义为非常量,C
问题描述
在我的标题中声明一个 const 变量是否有效,但将其定义为非常量变量以供内部使用?
解决方案
在头文件中声明变量的唯一合乎逻辑的方法是将它们声明为extern
由于#include
仅将头文件的文本插入源代码文件(https://godbolt.org/z/nor8nz),您可以简单地在单个源文件中测试您的想法:
extern const int x;
int x;
你会得到错误:
1
ARM gcc 8.2
- 347ms
<source>:4:5: error: conflicting type qualifiers for 'x'
int x;
^
<source>:2:18: note: previous declaration of 'x' was here
extern const int x;
^
Compiler returned: 1
推荐阅读
- ssl - Nginx PEM_read_bio_X509 PEM_read_bio:错误的结束行
- mysql - 当我在一张表中有数百万条记录时如何提高性能?
- python - 在 pd.read_excel 之后如何用通用索引更改索引
- java - 无法解析 @OneToMany 子表 (@JoinColumn) 的属性
- java - Java - FileOutputStream 覆盖文件,但它似乎没有改变
- maven - 从 pom 中的所有依赖项中排除特定文件
- translation - Symfony 5:将阿拉伯数字翻译成现代数字
- android - Android Button 背景无法识别/使用可绘制对象
- javascript - 如何在 React 功能组件中立即更新状态?
- mysql - Rails 升级到 5.2.3 导致“Mysql2::Error: Too many connections”