34
you are viewing a single comment's thread
view the rest of the comments
[-] tabular@lemmy.world 5 points 1 month ago
[-] Obin@feddit.org 20 points 1 month ago* (last edited 1 month ago)

You mean 'unnamed' is what's confusing you?

Normally you can do anonymous struct/union members or struct struct/union members that are tagged structs but not anonymous.

I.e. in standard C you'd have to do either:

struct foo { int baz; };
struct bar { struct foo foo; };
...
struct bar data;
data.foo.baz = 0;

or:

struct bar { struct {  int baz; } foo; };
...
struct bar data;
data.baz = 0;

but to do the following, you'd need the extension:

struct foo { int baz; };
struct bar { struct foo; };
...
struct bar data;
data.baz = 0;
[-] mina86@lemmy.wtf 8 points 1 month ago* (last edited 1 month ago)

~~Minor correction: Unnamed structs and unions (so your second example) are not part of C. They are GNU extensions.~~

[-] MinekPo1@lemmygrad.ml 3 points 1 month ago* (last edited 1 month ago)

"ANSI C" by Kernighan and Ritchie disagrees , including that syntax (note : retranslation from Polish as that's the language my copy is in) :

A8.3

[...]

struct-union-specifier:
, union-struct identifier ₒₚₜ { compound-declaration-list }
, union-struct identifier

[...]

Specifiers of structures or unions with [a compound declaration] list, but with no label [identifier], creates a unique type; it may only be referred to in the declaration in which it is part.

[-] mina86@lemmy.wtf 1 points 1 month ago

Yes, but I was talking about field name, not struct tag. And up to C99 my comment was correct.

[-] Obin@feddit.org 3 points 1 month ago* (last edited 1 month ago)

Unless I'm misunderstanding something, I'm pretty sure they've been standardized in C11. Also mentioned here.

[-] mina86@lemmy.wtf 1 points 1 month ago

You appear to be correct.

[-] mina86@lemmy.wtf 13 points 1 month ago* (last edited 1 month ago)

Tag is what goes after the struct keyword to allow referring to the struct type. Structs don’t have to have a tag. Name is what field are called. Adapting Obin’s example:

struct foo { int baz; };
struct bar { struct foo qux; };
struct bar data;
data.qux.baz = 0;

foo and bar are tags for struct foo and struct bar types respectively; baz and qux are field names; and data is a variable name.

this post was submitted on 10 Nov 2025
34 points (75.8% liked)

Linux

57274 readers
1009 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 6 years ago
MODERATORS