Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: C Programming. No Comments on Structures in C Programming
0
(0)

This article provides a comprehensive overview of structures in C programming language along with example programs.

 

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.

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


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:

 

struct  sturcturename/tagname
{
	datatype var1;
	datatype var2;
	----
	----
};

 

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:

 

struct student
{
	char name[20];
	char rollno[15];
	int age;
	char grade;
};

 

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:

 

struct  structurename  var1, var2, …., varN;

 

Example for creating structure variables is shown below:

 

struct student student1, student2, student3;

 

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

 

struct student
{
		char name[20];
		char rollno[15];
		int age;
		char grade;
}student1, student2, student3;

 

Type-Defined Structures

 

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

 

typedef  struct
{
	char name[20];
	char rollno[15];
	int age;
	char grade;
}student;

 

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

 

student student1, student2, student3;

 

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:

 

structure-varaible.membername

 

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

 

student1.name
student1.age

 

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:

 

struct student
{
		char name[20];
		char rollno[15];
		int age;
		char grade;
};
struct student student1 = {“teja”,”10A1”,26,’A’};
struct student student2 = {“raju”,”10A2”,24,’B’};

 

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:

gets(student1.name);
gets(student1.rollno);
scanf(%d”, &student1.age);
scanf(“%c”, &grade);

 

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:

 

struct textbook
{
		char name[40];
		char author[20];
		int pages;
}
struct textbook book[10];

 

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:

 

outer-struct-variable.inner-struct-variable.membername

 

An example for a nested structure is shown below:

 

struct student
{
		struct
		{
			char fname[20];
			char mname[20];
			char lname[20];
		}name;
		char grade;
};
struct student s1;
strcpy(s1.name.fname, “satish”);

 

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:

return-type function-name(struct structname var)
{
		----
		----
		----
		return expression;
}

 

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

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