Here we’ll learn about Structures, Typedef and Union in C Programming Language. If you recall from previous lesson, arrays are group of item of same type under one variable name. The Structures or struct is user-defined data type in C which allows grouping together related data items of different types. Structures are useful to construct a complex data type in more meaningful way. Structures are often used to represent record. Let’s take example, if we wish to store record of Book which consists of Book Name, No. of Pages and Price. We will define structure to hold this information of different types like name is string, No. of Pages will be integer and Price will be float value.
Table of Contents
Definition of Structure
The struct keyword is used to define structure. The struct define a new data type which is collection of different type of data. In our example Book is a structure.
[crayon-5c6ab8dd3e54e961532947/] This is how we can define structure. To use structure in C Programs, we have to declare structure variable. There are two ways we can do this. We can declare structure variable separately or with structure definition. Let’s have a look at each case.
Declaring Structure Variable separately[crayon-5c6ab8dd3e55c054565206/]
Declaring Structure Variable with Structure definition[crayon-5c6ab8dd3e562369901828/] Here B1 and B2 are variables of type Book. At the end of the structure’s definition before the final semicolon, we can specify one or more structure variables but it is optional. However, this second approach is not much recommended. In all of our example code we are going to declare structure variable separately.
Typedef and Structures in C Programming
The keyword Typedef is used to give a new symbolic name for the existing name. Typedef is type definitions make code more readable by giving application-specific names to types. In this example, we will create type definition with structures. The name of type definition of a structure is usually in upper case letters.
[crayon-5c6ab8dd3e571424999898/] Usually, Typedef will be placed into header file and use type names in main program. If definition of Book changes, we might not need to change the code in our main program file. Typedef interpretation is performed by compiler. There may be some reader confuse with #define and typedef. We will discuss their use cases in separate lesson.
Pointer to Structures
If we want a pointer to a structure we have to use the -> (arrow operator) instead of a dot. Take a look at the following example:
[crayon-5c6ab8dd3e578547916048/] Note: The -> (Arrow operator) is also used in the printf statement.
Union in C Programming
Unions are conceptually similar to Structures. The only difference between them is memory allocation. Structure allocates storage space for all its members separately; Whereas, Union allocates one common storage space for all its members. Members of a union can only be accessed one at a time. The union data type was invented to prevent memory fragmentation. The union data type prevents fragmentation by creating a standard size of certain data everything else remains same as Structures.
[crayon-5c6ab8dd3e57e227423153/] Let’s build and run this example code and observe output.
Here, we can see that the values of pages and price members of union got corrupted because the final value assigned to the variable has occupied the memory location and this is the reason that the value of name member is getting printed very well. As we know union can only access one member at a time. The following code will be corrected version of this example.
[crayon-5c6ab8dd3e585244352216/] Let’s build and run this example code and observe output. Here we will get expected output.
This is it for Structures, Typedef and Union in C Programming. We hope you have enjoyed reading this lesson. In next lesson, we’ll learn about Dynamic Memory Allocation in C Programming. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading.