c - 静态结构警告空声明中无用的存储类说明符
问题描述
static struct astr {
int a;
};
static const struct astr newastr = {
.a = 9,
};
我得到:警告:空声明中无用的存储类说明符
如果我将其更改为
static struct astr {
int a;
} something;
然后警告将被修复。
以下也没有给出警告
struct astr {
int a;
};
static const struct astr newastr = {
.a = 9,
};
有人可以解释这里发生了什么吗?
解决方案
当您有结构定义而没有声明任何变量时,您会收到警告。例如,以下将给出警告:
static struct s {
int a;
};
这相当于:
struct s {
int a;
};
它定义了结构s
,但不声明任何变量。即,没有与之关联的存储,所以没有什么可以应用的static
。
但如果你这样做:
static struct s {
int a;
} x;
然后没有警告,因为x
除了定义结构之外,您还声明了变量s
,因此static
适用于x
.
同样,如果struct s
之前已定义,您可以执行以下操作:
static struct s x;
没有任何警告。当然,如果需要,您可以选择提供初始化程序。
推荐阅读
- php - 重定向页面仅显示订单摘要(authorize.net 接受的主机)
- regex - 阻止 kubeconfig 文件提交到 GitHub
- sql - 用于翻译的数据库架构
- python - 如何将python脚本编译为无需shell即可打开的独立可执行Windows文件
- ruby-on-rails - 使用 When Gem 调用活动作业
- java - 用于列表目的的 Android 最有效容器
- c# - 数据表不一致
- javascript - 为什么用 ES5 表达的带有 set 关键字和函数的 setter 会抛出语法错误?
- python - 当多个元素具有最高计数时,pandas describe() - top 如何工作?
- twitter-bootstrap - 仅包含一个模块的引导样式