2
  static struct astr {
          int a;
  };

  static const struct astr newastr = {
          .a = 9,
  };

I get: warning: useless storage class specifier in empty declaration

If I change it to

  static struct astr {
          int a;
  } something;

then the warning will be fixed.

The following also does not give that warning

  struct astr {
          int a;
  };

  static const struct astr newastr = {
          .a = 9,
  }; 

Can someone explain what is going on here?

mch
  • 9,424
  • 2
  • 28
  • 42
user395980
  • 356
  • 1
  • 8

1 Answers1

5

You're getting warnings when you have structure definitions without declaring any variables. For example, the following will give a warning:

static struct s {
    int a;
};

This is equivalent to:

struct s {
    int a;
};

It defines the structure s but does not declare any variables. I.e., there is no storage associated with it, so there's nothing to apply the static to.

But if you do:

static struct s {
    int a;
} x;

Then there is no warning because you're declaring variable x in addition to defining structure s, so the static applies to x.

Similarly, if struct s has been previously defined, you can do:

static struct s x;

with no warning. And of course, you can optionally supply an initializer if desired.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41