Questions tagged [anonymous-struct]

In ISO C11 (and some extensions of ISO C++) an anonymous struct is a data member of struct type, whose members are treated as members of the enclosing struct or union. It is declared as `struct { /* members */ };`.

In C starting with the 2011 ISO standard (and some extensions of ISO C++) an anonymous struct is a data member of struct type, whose members are treated as members of the enclosing struct or union. It is declared as struct { /* members */ };.

Anonymous structs should not be confused with unnamed structs (which are an official C++ feature). That is a struct that has simply been given no name, such as in the following case:

// The type of "lineInfo" is an *unnamed struct*, 
// but "lineInfo" is _not_ an *anonymous struct*
struct {
   int lineNumber;
   int length;
} lineInfo;

In the following, the struct that contains byte0 and byte1 is anonymous, and those two members become members of the enclosing union, so that things.byte0 becomes valid:

union {
  struct {
    char byte0;
    char byte1;
  };
  char bytes[2];
} things;
35 questions
22
votes
4 answers

What does this pointer of type structure definition mean (in C)?

In K&R Chapter 6, a declaration is mentioned as follows: struct{ int len; char *str; } *p; I couldn't understand which structure is this pointer p pointing to and if such a pointer definition is even valid because in all other examples…
Eulerian
  • 382
  • 2
  • 8
13
votes
1 answer

C++ anonymous structs

I use the following union to simplify byte, nibble and bit operations: union Byte { struct { unsigned int bit_0: 1; unsigned int bit_1: 1; unsigned int bit_2: 1; unsigned int bit_3: 1; unsigned int bit_4: 1; unsigned int…
Dejwi
  • 4,393
  • 12
  • 45
  • 74
12
votes
1 answer

C++ private modifier ignored on nested anonymous struct

The following sample code compiles just fine in Visual C++: class Test { private: struct { struct { int privateData; }; }; }; int main(int, char **) { Test test; test.privateData = 0; return 0; } But…
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
9
votes
2 answers

Brace-or-equal-initializers in anonymous struct does not work on VS2013

Brace-or-equal-initializers in an anonymous struct within a struct doesn't do their work on output produced by VS2013. There's the code: #include #include struct S { struct { uint64_t val = 0; …
user2694310
8
votes
0 answers

compiler is out of heap space in pass 2 - anonymous union, anonymous structs, constexpr constructor, static member

I have code that fails to compile with Visual Studio 2015 Community Edition with the following error: fatal error C1002: compiler is out of heap space in pass 2 The Code struct Int { int i; }; struct B { union { struct { int x; }; …
Daskie
  • 435
  • 3
  • 11
6
votes
4 answers

C: How to access different types of anonymous or unnamed nested structs

I noticed that there are a few ways to define structs inside other structs in C: struct s { int abc; struct { int a; }; struct { int b; } intern; struct i { int c; }; struct i2 { …
PieterV
  • 816
  • 10
  • 23
6
votes
1 answer

Is this a C11 anonymous struct?

I was looking into the C11 draft and it says 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…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
6
votes
4 answers

Dealing with C library anonymous struct types in C++

We have a big, old C++ application with a lot of legacy code and a few external libraries written in C. These libraries are very rarely updated - only if we find a bug and the vendor supplies a patch. This happened last week with one library, and…
l4mpi
  • 5,103
  • 3
  • 34
  • 54
5
votes
1 answer

How to fix compilation errors in MSVC related to declaration of anonymous struct inside a 'for' loop?

We can declare anonymous struct inside for loop as below (g++): for(struct { bool OK = true; } s; s.OK; s.OK = false) std::cout << "Hello, world!\n"; But, this code results in compilation error in MSVC as: source_file.cpp(7): error C2332:…
iammilind
  • 68,093
  • 33
  • 169
  • 336
5
votes
1 answer

C11 - convert pointer-to-struct to struct's anonymous first member

The C standard states: A pointer to a structure object, suitably cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. Is there any possible (and well-defined) way to perform…
user3079266
4
votes
2 answers

Can you declare an anonymous instance of a named struct?

I'm trying to manually implement a polymorphic behavior in C by creating a generic struct, and then derived structs (if you will) which can be told apart by the value of an enum, so that I can have a pointer to the generic type, dereference it as…
Willis Hershey
  • 1,520
  • 4
  • 22
4
votes
1 answer

Does dereferencing a cast to an anonymous structure pointer violate strict aliasing?

I have heard conflicting things about the extent to which the C standards guarantee structure layout consistency. Arguments for a limited extent have mentioned strict aliasing rules. For example, compare these two answers:…
nebuch
  • 6,475
  • 4
  • 20
  • 39
3
votes
1 answer

C++: struct forward declaration of anonymous struct causes "conflicting declaration"

I have got this hpp file: struct rte_spinlock_t; class A { public: void init(); private: rte_spinlock_t* spinlock; }; and the corresponding cpp file: #include "A.hpp" typedef struct { int lock; }…
3
votes
3 answers

Anonymous struct with ANSI C

I want to know if it is possible to declare anonymous structs in ANSI C. The code I have is: struct A { int x; }; struct B { struct A; int y; }; When I compile it I get: warning: declaration does not declare anything I have read that…
Klas. S
  • 650
  • 9
  • 21
2
votes
0 answers

cgo -godefs with nested anonymous structs?

I'm writing some go code that will take the output of a sysctl and parse the output. I'm trying to do this in pure go, but with a struct definition generated by cgo. inet_headers.go: //go:build ignore // +build ignore package inet // #include…
craig65535
  • 3,439
  • 1
  • 23
  • 49
1
2 3