Structure padding and packing

Padding aligns structure members to “natural” address boundaries – say, int members would have offsets, which are mod(4) == 0 on 32-bit platform. Padding is on by default. It inserts the following “gaps” into your first structure: Packing, on the other hand prevents compiler from doing padding – this has to be explicitly requested – under GCC it’s __attribute__((__packed__)), so the following: … Read more

How to print struct variables in console?

To print the name of the fields in a struct: From the fmt package: when printing structs, the plus flag (%+v) adds field names That supposes you have an instance of Project (in ‘yourProject‘) The article JSON and Go will give more details on how to retrieve the values from a JSON struct. This Go by example page provides another technique: … Read more

Initializing array of structures

There’s no “step-by-step” here. When initialization is performed with constant expressions, the process is essentially performed at compile time. Of course, if the array is declared as a local object, it is allocated locally and initialized at run-time, but that can be still thought of as a single-step process that cannot be meaningfully subdivided. Designated … Read more

What does “request for member ‘*******’ in something not a structure or union” mean?

It also happens if you’re trying to access an instance when you have a pointer, and vice versa: As pointed out in a comment, this can be made excruciating if someone goes and typedefs a pointer, i.e. includes the * in a typedef, like so: Because then you get code that looks like it’s dealing with instances, when in fact … Read more

What does “request for member ‘*******’ in something not a structure or union” mean?

It also happens if you’re trying to access an instance when you have a pointer, and vice versa: As pointed out in a comment, this can be made excruciating if someone goes and typedefs a pointer, i.e. includes the * in a typedef, like so: Because then you get code that looks like it’s dealing with instances, when in fact … Read more

Function for C++ struct

Yes, a struct is identical to a class except for the default access level (member-wise and inheritance-wise). (and the extra meaning class carries when used with a template) Every functionality supported by a class is consequently supported by a struct. You’d use methods the same as you’d use them for a class.