3 ms·
> But what if we want a struct to feel like a real C type? Remove this; it's already a real type. You could go over the two namespaces and why you need to type
by juunpp 3y ago
> But what if we want a struct to feel like a real C type?
Remove this; it's already a real type. You could go over the two namespaces and why you need to type 'struct' otherwise, but I wouldn't present a false dichotomy of real and "unreal" types.
> Declaring a Struct as a New Type
It's not really a new type. It's a type alias, and the two names can be used interchangeably. I would just call it a type alias.
https://en.cppreference.com/w/c/language/typedef https://en.cppreference.com/w/c/language/typedef
> If we declare a structure variable without initializing it, like any other variable in C, it will be uninitialized at first and may contain random values.
I also see no mention of = {} or = {0} (I forget which is Standard C and which is C++). Since you already talk about "random values", you might as well explain how to 0-initialize a struct.
- dropbear3 3y ago{0} is standard C. {} is currently a (common) compiler extension but will be standard C23: https://open-std.org/JTC1/SC22/WG14/www/docs/n2900.htm https://open-std.org/JTC1/SC22/WG14/www/docs/n2900.htm
- dundarious 3y agoYes, it's a bit frustrating, especially for headers with inline/macro code. And for headers, requiring C23 doesn't seem sensible for quite some time. I define a macro: #ifdef __cplusplus # define ZERO_INIT {} #else # define ZERO_INIT {0} #endif Works for arrays, aggregates, scalars, etc., but I just use it for arrays and aggregates: `char buf[32] = ZERO_INIT; struct X x = ZERO_INIT;`