【问题标题】:Static struct warning useless storage class specifier in empty declaration空声明中的静态结构警告无用的存储类说明符
【发布时间】:2020-04-07 11:44:19
【问题描述】:
  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,
  }; 

有人能解释一下这里发生了什么吗?

【问题讨论】:

标签: c struct static


【解决方案1】:

当您有结构定义但未声明任何变量时,您会收到警告。例如,以下将给出警告:

static struct s {
    int a;
};

这相当于:

struct s {
    int a;
};

它定义了结构s,但没有声明任何变量。即,没有与之关联的存储,因此没有任何东西可以应用 static

但如果你这样做:

static struct s {
    int a;
} x;

然后没有警告,因为除了定义结构s之外,您还声明了变量x,因此static适用于x

同样,如果struct s之前已经定义了,你可以这样做:

static struct s x;

没有警告。当然,如果需要,您可以选择提供初始化程序。

【讨论】:

    猜你喜欢
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 2021-12-18
    • 2018-01-09
    相关资源
    最近更新 更多