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?

MFrancone
  • 161
  • 1
  • 10
  • 3
    You can't declare struct members as static in C. – DeiDei May 31 '18 at 13:43
  • 1
    It would seem that the book is really bad. – Lundin May 31 '18 at 13:55
  • 2
    Are you sure the book was using C and not C++? – Some programmer dude May 31 '18 at 13:56
  • Also, `struct { /* Something or other */ } myVariable;` defines an *anonymous* structure, and then defines a variable `myVariable` using this anonymous structure. Or perhaps you missed a `typedef` before `struct`? Because `typedef struct { /* ... */ } myTypename;` defines an anonymous structure and then make a type alias to use the structure. – Some programmer dude May 31 '18 at 13:58
  • Lastly, you seem to have two different questions in this question, which makes it to broad. Please keep it to one question per question. – Some programmer dude May 31 '18 at 13:58
  • 1
    @Someprogrammerdude: *Anonymous struct* is reserved for the case where you have an *unnamed struct* inside another one. – Acorn May 31 '18 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. – Some programmer dude May 31 '18 at 14:01
  • @Someprogrammerdude: I tried to look up a reference for that. It seems in the C99 Standard there is no usage of "anonymous" (there is of "unnamed", though). In the C++ Standard, however, they define "anonymous union": "it defines an unnamed type and an unnamed object of that type called an anonymous union object". Also, see https://stackoverflow.com/questions/14248044/are-anonymous-structs-standard-and-really-what-are-they (for C++) – Acorn May 31 '18 at 14:12
  • 1
    The [C11 standard §6.7.2.1 ¶2 Structure and union specifiers](https://port70.net/~nsz/c/c11/n1570.html#6.7.2.1p2) says: _A struct-declaration that does not declare an anonymous structure or anonymous union shall contain a struct-declarator-list._ (¶13 is also highly relevant.) Thus, in C11 (but not C99 or C90), there are anonymous structures and unions. – Jonathan Leffler May 31 '18 at 14:14
  • 1
    @Someprogrammerdude: In the C11 standard (6.7.2.1.13), though, they say: "An unnamed member of structure type with no tag is called an anonymous structure; an unnamed member of union type with no tag is called an anonymous union. The members of an anonymous structure or union are considered to be members of the containing structure or union. This applies recursively if the containing structure or union is also anonymous." – Acorn May 31 '18 at 14:15
  • @Acorn "Anonymous" or "unnamed" doesn't matter, it's still allowed to declare variables using "unnamed" structures in any scope or context where variable definitions are allowed. The `struct { ... }` is simply the type, like `int` or `struct some_named_structure`. – Some programmer dude May 31 '18 at 14:17
  • @Someprogrammerdude: My comment was only pointing out that your example `struct { /* Something or other */ } myVariable;` defines an unnamed structure, not an anonymous one. I thought it was a good idea to clarify it since this question is specifically asking about unnamed structures. In other words, I didn't mean to nitpick. – Acorn May 31 '18 at 14:25
  • @Acorn It's okay. English is not my native language, so there might be nuances between "anonymous" and "unnamed" that I don't know about, even though I consider them synonymous. – Some programmer dude May 31 '18 at 14:32
  • What book did you find this in? – John Bode May 31 '18 at 15:38
  • author: https://www.youtube.com/watch?v=E3avy280G6I – purec May 31 '18 at 15:40
  • @John Bode, Elecia White "Making Embedded Systems" – purec May 31 '18 at 15:44
  • @Someprogrammerdude yes I'm sure it's talking about C – MFrancone May 31 '18 at 17:06
  • @JohnBode yes it's "Making Embedded System". It's a good book w.r.t concepts, not about programming languages! – MFrancone May 31 '18 at 17:49
  • @MFrancone: Yeah, even good references have mistakes in them - stuff just manages to get past editors and proofreaders. I was simply curious where the OP found it. – John Bode May 31 '18 at 18:07

2 Answers2

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.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • 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 May 31 '18 at 17:52
  • @MFrancone thanks for the hint. For me the words were synonyms until now. I updated the answer. – Gerhardh May 31 '18 at 18:11
  • 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. – MFrancone May 31 '18 at 23:35
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;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Acorn
  • 24,970
  • 5
  • 40
  • 69