1

I've found the following example in a technical book

struct { tBoolean logON; static enum eLogLevel outputLevel[NUM_LOG_SUBSYSTEM]; } sLogStruct; static struct sLogStruct gLogData; 

but I was confused about the goodness of the struct declaration that, formally, should rather be

struct name { ... } 

in fact, I make some trial and the compiler comes out with an error at the statement (I really tried with standard types and not with something like sLogStruct)

static struct sLogStruct gLogData; 

Is my doubt right and the code is faulty?

In addition I'd like to understand the meaning and the scope of static class storage within a struct as I did't find any satisfying explanation. Let's have a struct declaration like this

struct myStruct { int Foo; static int sFoo; } strA, strB; 

does strA and strB have two static variable indipendent to one another?

19
  • 3
    You can't declare struct members as static in C. Commented May 31, 2018 at 13:43
  • 1
    It would seem that the book is really bad. Commented May 31, 2018 at 13:55
  • 2
    Are you sure the book was using C and not C++? Commented May 31, 2018 at 13:56
  • 1
    @Someprogrammerdude: Anonymous struct is reserved for the case where you have an unnamed struct inside another one. Commented May 31, 2018 at 14:00
  • 1
    @Acorn Why would they be reserved for nested structures only? You could use it to define any structure variable anywhere just like any other variable. Commented May 31, 2018 at 14:01

2 Answers 2

1

Is my doubt right and the code is faulty?

Yes, somehow the code is faulty.

Let's take a closer look:

struct { tBoolean logON; static enum eLogLevel outputLevel[NUM_LOG_SUBSYSTEM]; } sLogStruct; 

Let's assume you define the types of the members somewhere.

You declare an unnamed struct type and define a variable sLogStruct of that type. There is no struct tag and no type name defined.

static struct sLogStruct gLogData; 

As there is no type struct sLogStruct, you cannot define a variable of that type.

In addition I'd like to understand the meaning and the scope of static class storage within a struct as I did't find any satisfying explanation.

There is no meaning. For struct members it does not make any sense to add static keyword and it is not allowed.

Sign up to request clarification or add additional context in comments.

3 Comments

Based on the above comments I think that the struct declaration brings to an unnamed struct rather a anonymous one. Apart of this, your answer has been helpful!
@MFrancone thanks for the hint. For me the words were synonyms until now. I updated the answer.
I neither knew anonymous nor unnamed until now! Try to get a glance to the link posted in the @jonathanLeffler comment. Paragraph 13 and the examples are quite clear.
0

The tag name of a struct is optional.

For instance, the following would declare two variables (s1, s2) of an unnamed struct type:

struct { int a; } s1, s2; void f() { s1.a = 3; s2.a = 5; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.