As others have said, a structure is simply a grouping of variables. Depending on your purpose, you may need to consider registration or packaging if you want to access elements through low-level functions (for example, using pointer arithmetic): structure elements are usually aligned at 4 byte boundaries (32 bits). Thus, a structure that includes elements of different sizes may be required to complement
struct foo { int a; char b; int c; char d; }
In this example (assuming int is 4 bytes and char is 1 byte, and the CPU is aligned with 32-bit boundaries), you need 3 bytes of padding after b to align the structure. In this case, it is more efficient to sort the structure in different ways. Please note that this will not change its use, since it does not change the names of participants. Not all processors need a structure that needs to be aligned, but using elements of a packaged structure to save some space can result in a speed penalty. In most cases, you have nothing to worry about.
As with typedef around a structure, you can use the same name for typedef and struct, i.e. something like that:
typedef struct foo { int a; int b; } foo;
This allows you to use
struct foo variable;
or
foo variable;
to declare a variable of type struct foo .
From my point of view, typing a structure is a bad idea, as it hides the information that typedef (e.g. foo ) is a structure, and writing the struct keyword is not really an additional work (especially if these typedefs add a postfix so that indicate that they are a structure - I saw this quite often).
Edit : in the second example, there is no typedef .
bluebrother
source share