Structure in C Structure . is a group of variables of different data types represented by a single name Structure is a user-defined datatype in C language It is somewhat similar to an Array-an array holds data of similar type only. But structure can store data of any type Problem: we need to store the data of students like student name, age, address, id etc. One way of doing this would be creating a different variable for each attribute, however when you need to store the data of multiple students then in that case, you would need to create these several variables again for each student. This is such a big headache to store data in this way. Solution: We can solve this problem easily by using structure. We can create a structure that has members for name, id, address and age and then we can create the variables of this structure for each student. In structure, data is stored in form of records. How to define structures? struct keyword is used to define a structure. struct defines a new data type which is a collection of primary and derived data types. The data fields in structure are called structure elements or members. Syntax of struct Example: Declare variable of a structure When a struct type is declared, no storage or memory is allocated. To allocate memory of a given structure type and work with it, we need to create variables. It is possible to declare variables of a structure, either along with structure definition or after the structure is defined. Structure variables can be declared in following two ways: 1. Declaring Structure variables separately 2. Declaring Structure variables with structure definition Accessing Structure Members the member name must be linked with the structure variable using a dot . operator also called period or member access operator. There are two types of operators used for accessing members of a structure. (. )Dot operator -> Structure pointer operator (will be discussed later) Structure Initialization Like a variable of any other datatype, structure variable can also be initialized at compile time. Example: Example: We can also use scanf() to give values to structure members through terminal. Array of Structure We can also declare an array of structure variables. in which each element of the array will represent a structure variable. Example : struct employee emp[5]; The below program defines an array emp of size 5. Each element of the array emp is of type Employee. Nested Structure in C: (Struct inside another struct) Nested structures means, that one structure has another stucture as member variable. Structure 2: stu_data Structure 1: stu_address struct stu_address { int street; char *state; char *city; char *country; } struct stu_data { int stu_id; int stu_age; char *stu_name; struct stu_address stuAddress; } Assignment for Nested struct: How to access nested structure members? struct stu_data mydata; mydata.stu_id = 1001; Using chain of “.” operator. Suppose you want to display the city alone from nested struct – mydata.stu_age = 30; mydata.stuAddress.state = "UP"; //Nested struct assignment .. printf("%s", mydata.stuAddress.city); Nested structure can be defined as given below: Passing structs to functions in C We can pass a structure as a function argument just like we pass any other variable or an array as a function argument. 1. Passing structure to a function by value 2. Passing structure to a function by address(reference) 1. Passing structure to a function by value Passing structure to a function by address(reference) #include <stdio.h> #include <string.h> int main() { struct student record; struct student { int id; char name[20]; float percentage; }; void func(struct student *record); record.id=1; strcpy(record.name, "Raju"); record.percentage = 86.5; func(&record); return 0; } void func(struct student *record) { printf(" Id is: %d \n", record->id); printf(" Name is: %s \n", record->name); printf(" Percentage is: %f \n", record->percentage); } typedef typedef makes the code short and improves readability. while using structs every time we have to use the lengthy syntax, which makes the code confusing, lengthy, complex and less readable. The simple solution to this issue is use of typedef. It is like an alias of struct. Code without typedef Code using tyepdef struct home_address typedef struct home_address { int local_street; { int local_street; char *town; char *town; char *my_city; char *my_city; char *my_country; char *my_country; }; }addr; ... .. .. struct home_address var; addr var1; var.town = "Agra"; var.town = "Agra"; Example: Unions A union is a user-defined type similar to structs in C except for one key difference. Structures allocate enough space to store all their members, whereas unions can only hold one member value at a time. In structure each member has its own storage location, whereas all members of union uses a single shared memory location which is equal to the size of its largest data member. C unions are used to save memory. When we want to assign a new value to a field, then the existing data is replaced with new data. Difference between unions and structures Here, the size of sJob is 40 bytes because the size of name[32] is 32 bytes the size of salary is 4 bytes the size of workerNo is 4 bytes However, the size of uJob is 32 bytes. It's because the size of a union variable will always be the size of its largest element. In the above example, the size of its largest element, (name[32]), is 32 bytes. With a union, all members share the same memory. although a union may contain many members of different types, it cannot handle all the members at the same time. A union is declared using the union keyword. How to define a union? In the union declared above the member x requires 4 bytes which is largest amongst the members for a 16-bit machine. Other members of union will share the same memory address. Create union variables (2 ways:) Access members of a union: • We use the . operator to access members of a union. • And to access pointer variables, we use the -> operator. To access price for car1, car1.price is used. To access price using car3, either (*car3).price or car3->price can be used. Example: 1 The values of a and b get corrupted and only variable c prints the expected result. This is because in union, the memory is shared among different data types. Hence, the only member whose value is currently stored will have the memory. In the above example, value of the variable c was stored at last, hence the value of other variables is lost. Example 2: enum in C An enumeration(enum) is a special class that represents a group of constants. It is used to assign names to the integral constants, which makes a program easy to read and maintain. The enum keyword is used to create an enum. The constants declared inside are separated by commas. An enum variable can take only one value. Example 1: By default, the first item in the list is assigned the value 0, the second item is assigned the value 1, and so on. Example 2: Assigning custom values to enum elements Where to use enum enum is used for values that are not going to change (e.g., days of the week, colors in a rainbow, number of cards in a deck, etc.). enum is commonly used in switch-case statements. The code below shows an example: Example 2: Example 3: