My Blog
Articles related to programming, computer science, technology and research.

23/04/2016 Categories: C Programming. No Comments on Structures in C

We know that arrays can be used to represent a collection of elements of the same data type like int, float etc. They cannot be used to hold a collection of different types of elements.


C supports a structured data type known as a structure, a way for packing data of different data types. A structure is a convenient tool for handling a group of logically related data items. For example, it can be used to represent the details of a student or the details of a text book etc.


Definition: A structure is a collection of heterogeneous data elements referred by the same name.


Defining a Structure


Unlike arrays, structures must be first defined first for their later use. The syntax for defining a structure is as shown below:



The keyword struct declares a structure. The structurename / tagname represents the name of the structure. The structure definition is always terminated with a semicolon.


Let us see an example for defining a structure to store the details of a student:



In the above example, student is the name of the structure. The members of the student structure are: name, rollno, age and grade. A structure itself does not occupy any memory in the RAM. Memory is allocated only when we create variables using the structure.


Declaring Structure Variables


After defining a structure, we can create variables of that type. A structure variable declaration is similar to the declaration of variables of any other data types. It includes the following elements:


  1. The keyword
  2. The structure name or tagname.
  3. List of variable names separated with commas.
  4. A terminating semicolon.


The syntax for declaring structure variables is as shown below:



Example for creating structure variables is shown below:



We can also combine the structure definition and the declaration of structure variables into a single line as shown below:



Type-Defined Structures


We can use the keyword typedef to define a structure as follows:



The name student represents the structure definition associated with it and therefore can be used to declare structure variables as shown below:



Accessing Structure Members


We can access and assign values to the members of a structure in a number of ways. As mentioned earlier, the members themselves are not variables. They should be linked to the structure variables in order to make them meaningful members.


The link between a member and a variable is established using the member operator ‘ . ‘ which is also known as dot operator or period operator. The syntax for accessing a structure member is as shown below:



Examples of accessing the members of the student structure are shown below:



Structure Initialization


Like any other data type, a structure variable can be initialized at compile time. An example of compile time initialization of the student structure is shown below:



In the above example: teja, 10A1, 26 and A are assigned to student1’s name, rollno, age and grade. Whereas raju, 10A2, 24, B are assigned to student2’s name, rollno, age and grade.


Note: Partial initialization of a structure variable is supported. For example we can assign values to name and rollno and leave out age and grade. In case of partial initialization, the default values for int type is zero. For float and double it is 0.0. For characters and strings it is ‘\0’.


An example for dynamic initialization is shown below:



Arrays of Structures


We use structures to describe the format of a number of related variables. For example, we want to store details of 100 textbooks it will become difficult to declare and maintain 100 variables and store the data. Instead, we can declare and array of structure variables as shown below:



In the above example, we are declaring an array book with 10 elements of the type textbook which is a structure.


Structures within Structure


In C, structures can be nested. A structure can be defined within in another structure. The members of the inner structure can be accessed using the variable of the outer structure as shown below:



An example for a nested structure is shown below:



In the above example, student is the outer structure and the inner structure name consists of three members: fname, mname and lname.


Structures and Functions


We know that the functions are the basic building blocks of a C program. So, it is natural for C language to support passing structures as parameters in functions. We pass a copy of the entire structure to the called function.


Since the function is working on a copy of the structure, any changes to structure members within the function are not reflected in the original structure.


The syntax for passing the structure variable as a parameter is shown below:



Related Links:

Popular Posts:

Leave a Reply

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

Scroll Up