<aside> 💡 Notion Tip: Add more details right in this page, including toggles, images, and webpage links, and more. Break up the page by using our different header options so that it’s easier to read. Learn more about different types of content blocks here.
</aside>
Structures are user-defined data types that allow the programmer to group related data items of different data types into a single unit. Structs can be used to store data related to particular object. Each item within a struct is called as a member or element.
A common occurrence is in Windows APIs.
Structures are declared with the use of typedef keyword to give structure an alias. For example -
typedef struct _STRUCTURE_NAME {
// stucture elements/members
} STRUCTURE_NAME, *PSTRUCTURE_NAME;
The structure above is created with the name _STRUCTURE_NAME but typedef adds two other names, STRUCTURE_NAME(alias to the structure name) & *PSTRUCTURE_NAME(pointer to that structure).
STRUCTURE_NAME struct1 = { 0 }; // Initialize all members of struct1 to zero.
// or
struct _STRUCTURE_NAME struct1 = { 0 }; //Same
// In case of pointer
PSTRUCTURE_NAME structpointer = NULL;
typedef struct _STRUCTURE_NAME {
int ID;
int Age;
} STRUCTURE_NAME, *PSTRUCTURE_NAME;
STRUCTURE_NAME struct1 = { 0 }; //initializing all elements to zero
struct1.ID = 0915;
struct1.Age = 3;
// Another way
STRUCTURE_NAME struct1 = {.ID = 0915, .Age = 3};
// Through it's pointer
STRUCTURE_NAME struct1 = { 0 }; //initializing all elements to zero
PSTRUCTURE_NAME structpointer = &struct1;
structpointer->ID = 2024 // Equivalent to (*structpointer).ID
printf("The structure's ID member is now: %d \\n", structpointer->ID);
Enumeration datatype is used to define a set of named constants. Can be used to represent specific data, error codes or return values. Enum list cannot be modified or accessed using the dot (.) operator. Instead, each element is accessed directly using it’s named constant value.
enum Weekdays {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
enum Weekdays EnumName = Friday;
switch (EnumName){
case Monday:
printf("Today is Monday\\n");
break;
default:
printf("Today is not Monday\\n");
break;
}
Union is a data type that permits storage of various data types in the same memory location. A single memory location can be used for multiple purposes. Commonly seen in Windows-defined structures. Eg -
union ExampleUnion {
int IntegerVar;
char CharVar;
float FloatVar;
};
Access can be done using dot method. Assigning value to any 1 element will replace all the other elements as they are in the same memory location. Size = largest member in the union.