Structure in C – Part 3

Read: Structure in C – Part 2

In the last two tutorials I gave a brief overview and functions of structures in C programming. Structure is one of the most important topic of C programming. The concept of structure is re-modified to make the object oriented language C++. Today I will tell you about the various advance features of structures in C programming. I will also tell you about the uses of structures at the end of this tutorial. So lets get started.

Structure in C

1. C language gave user the power to create your own variables. Structures are generally used to create user defined data types. To make the functionality of structures similar to other variables, the elements of structures are stored in contiguous memory locations. Due to this, one can easily assign all the values of one structure variable to other structure variable.

Remember we can also do piece meal copy by copying every element of structure one by one. Lets make one program to understand this.


Output

Structure in C - Part 3

2. Nesting is one main feature in C which gives the flexibility to create complex programs. Structures can also be nested to create some complex structures. Generally programmers prefer to stay within 3 layers of nesting. But remember practically there is no limit of nesting in C. Consider the below program to understand this.


Output

Structure in C - Part 3

Explanation
Nesting of structure is quite simple. Consider carefully the two dot operators I have used inside printf function to access the elements of first structure.

3. So far I told you about the pointer to integer, pointer to character, pointer to array and so on. Similar to this we can also make pointer to a structure. It is used to store the address of a structure. Remember we cannot use (.) dot operator to access the structure elements using structure pointer. We have to use arrow operator (->) for that purpose. Consider the below code.

Output

Structure in C - Part 3

Explanation
Consider carefully I have used arrow operator to access elements at that address

Uses of Structure in C

Structures are generally used to maintain databases in some organisation. One separate concept of Data Structures is also made to maintain the databases using structures. Apart from this some very common uses are given below.

a) Formatting a floppy
b) Receiving a key from the keyboard
c) Moving the cursor on the display
d) Making small games like Tic Tac Toe
e) Sending the output to printer

Leave a Comment

Your email address will not be published. Required fields are marked *