Structures in C

Definition of structure

A structure is a collection of variables, probably of different data types, grouped together under a single name. Each variable in structure is called member of the structure. A structure is one of the user defined data types. In other words a structure is like a record in which each member in structure refers to each column in the record.

How to Declare a Structure ?

Syntax:

struct structure_name
{
data_type variable_name1;
data_type variable_name2;
data_type variable_name3;
.
.
.

data_type variable_namenN;
};

Example:

struct employee
{
int employee_id;
char employee_name[20];
char address[50];
float salary;
};
We can also declare a structure using typedef keyword. The benefit of defining structure with typedef is that whenever we want to create a new structure variable there is no need to use the struct keyword.
typdef struct employee
{
int employee_id;
char employee_name[20];
char address[50];
float salary;
}emp;
data_type_name and structure_name can declared same. Now we can use the new user defined data type created like any other built in data type in c programming language.

Example:

// emp is the name of new data type
emp employee1; // creates a single employee record
emp employee[100]; // creates an array of employee records

Creating structure variables

Before we can use the structure declared above we need to create its variable like any other data type.

Syntax:

struct structure_name variable_name;

Example:


struct employee emp;

How to access the structure members ?

To access a member in the structure we use dot (.) operator.

Syntax:

structure_variable_name.member_name;

Example:

employee.employee_id;
employee.employee_name;
employee.address;
employee.salary;

How to initialize a structure variable ?

We use curly braces { } to initialize a structure variable with some default values.

Example:

struct employee emp={101,"Sam","Down Street,NY",1000.00};
Remember we need to set values in the order the members are declared in the structure. For example, in our employee structure we declared members in the order id, name, address and salary so we have initialized in that order.

Pointer to a structure variable

We can also declare a pointer to a structure variable.

Syntax:

struct structure_name pointer_name;

Example:

struct employee *ptr;
//pointing the structure pointer to a structure variable
*ptr=&employee; 

How to access structure members using pointer ?

Syntax:

pointer_name->member_name;

Example:

ptr->employee_name;
ptr->employee_id=102; // As we can access we can also set values
ptr->salary=12000.00;

How to declare array of structures ?

Syntax:

struct structure_name array_name[size];

Example:

struct employee emp[100]; // it can store 100 employee records

How to pass structures to functions ?

Syntax:

return-type function_name(struct structure_name variable_name);

Example:

void show_data(struct employee emp);

How to pass array of structures to functions ?

Syntax:

return-type function_name(struct structure_name array_name[size]);

Example:

void show_data(struct employee emp[100]);

Example programs on structures:

  1. C programming examples on structures