Structures in C: From Basics to Memory Alignment

Structures in C allow us to combine variables to create new data types, similar to the concept of “records” in other languages. They can be thought of as classes without methods in object-oriented programming. Structures are declared using the keyword “struct” followed by the name and a list of its members. To use a structure, we declare a new variable of that structure type and access individual members using the dot syntax. If we want to access a structure via a pointer, we use the “->” operator. The C99 Standard introduced designated initializers for initializing structure members explicitly. C does not support the comparison of structs using the “==” operator, and structs cannot contain members of their own type. Structures can be nested, and typedef can be used to declare a struct as a new type. Structures can be allocated dynamically on the heap using malloc, and it is important to free the allocated memory to avoid memory leaks. Before C99, structs could only be passed by reference using pointers, but C99 allows structs to be passed by value. Structures can also be copied and compared using member by member comparison. The memory layout of structures includes alignment and padding to ensure efficient memory access.

https://abstractexpr.com/2023/06/29/structures-in-c-from-basics-to-memory-alignment/

To top