Handling Data Using Structures

Learn structures and their basic syntax with the help of an interactive example.

Introduction to structures

As we know, arrays do not permit the collection of dissimilar elements. To gather dissimilar elements together, we need to use a data type called structure.


A structure is a user-defined data type that allows us to store data of dissimilar types.


Declaring a structure

The basic syntax for declaring a structure in C is given below:

The program given below declares a structure, employee, using a keyword called struct. The names, ages, and salaries are the members, or elements, of the structure, employee.

C
# include <stdio.h>
int main( )
{
// Declares structure employee
struct employee
{
// Members of structure
char name ;
int age ;
float salary ;
} ;
return 0;
}

📝Note: A structure is usually a collection of dissimilar elements.

Creating a structure variable

After defining the structure, we can create a variable of its type using the following syntax:

struct structName structVariable;

Once the structure is declared, we have to create variables of its type. In our case, these variables are e1, e2, e3, and e4.

C
# include <stdio.h>
int main( )
{
// Declares structure employee
struct employee
{
// Members of structure
char name ;
int age ;
float salary ;
} ;
// Declares structure varaible
struct employee e1, e2, e3;
return 0;
}

Assigning values to structure members

Method 1: assign a value to a single member

We can use the dot operator to access the particular member of the structure variable and assign it a value. . is known as a member access operator.

structureVariable . memberVariable = value;

The program given below assigns values to the members of e1 by using the dot operator.

C
# include <stdio.h>
int main( )
{
// Declares structure employee
struct employee
{
// Members of structure
char name ;
int age ;
float salary ;
} ;
// Declares structure varaible
struct employee e1, e2, e3;
// Assigning values to structure members
e1.name = 'A';
e1.age = 23;
e1.salary = 4000.50;
// Printing values of structure members
// Members of structure variable can be accessed using dot operator
printf ( "%c %d %f\n", e1.name, e1.age, e1.salary ) ;
return 0;
}

Method 2: assign values to all members

Using the initializer list, we can assign values to all the structure members in one line.

structureVariable = { member1Value , member2Value ,.........., member(n)Value };

The program given below uses the initializer list to assign values to all the members of e1, e2, e3, and e4, respectively.

C
# include <stdio.h>
int main( )
{
// Declares structure employee
struct employee
{
// Members of structure
char name ;
int age ;
float salary ;
} ;
// Assigning values to structure members in one line
struct employee e1 = { 'A', 23, 4000.50 } ;
struct employee e2 = { 'X', 27, 5000.00 } ;
struct employee e3 = { 'Y', 28, 6000.75 } ;
// Printing values of structure members
printf ( "%c %d %f\n", e1.name, e1.age, e1.salary ) ;
printf ( "%c %d %f\n", e2.name, e2.age, e2.salary ) ;
printf ( "%c %d %f\n", e3.name, e3.age, e3.salary ) ;
return 0;
}

📝 Note: A structure is also known as a user-defined data type, an aggregate data type, a secondary data type, or a derived data type.