C Structures ( Structs) | Structure in C | C Struct (Structures )

A structure is a heterogeneous set of data items. That is, a structure is a set of data items belonging to different data types. It is a group of variables of different data types referenced by a single name. A structure is a convenient way of grouping several pieces of related information together.

In C programming, structure is a collection of different data items which are referenced by single name. It is also known as user-defined data-type in C. A struct (or structure) is a mixed data type in C. You can use it to store variables of different types.

Structures are used for storing a closely related data items, such as all information about the student. The definition of structure forms a template that is used to create variables of a structure. Before we create structure variable, we need to declare the structure.

To declare a structure we must use the keyword struct, followed by the structure name.  Inside the structure, you can specify variables of different types:


The general Syntax of declaring a structure is:

 

struct struct_name

{

data_type member1;

data_type member2;

…………………..;

…………………..;

};

 

The keyword struct declares a structure to hold the template, struct_name is name of the structure, data_type indicates the type of the data members of the structure, member1, member2 etc. are the name of the data member of the structure.

Structure name is called the tag name. Each item declared in the template is called a field or member of the structure. Each member is a data item that can belong to any data type.

A structure declaration does not declare any variables. It declares a format or template. As no structure variable has been declared, so no memory space has been allocated. It is only the definition of the form of the data.

The structure declaration must end with a semicolon.


Example of declaring a structure:

 

struct student

{

char name[10];

int roll_no;

int age;

float fees;

};

The above declaration defines student as the structure name (tag-name).



Declaring structure variable:

Declaration of a structure does not allocate any space in memory. To allocate memory, we need to create structure variables.

Structure variables can either be declared with structure declaration or as a separate declaration.



Declaring structure variables with structure declaration,

 

struct student

{

char name[10];

int roll_no;

int age;

float fees;

} std1,std2;

Here, std1 and std2 are variables of structure student.

 


Declaring structure variable separately


struct student

{

char name[10];

int roll_no;

int age;

float fees;

};

struct student std1, std2;



Accessing structure members:

To access any member of a structure, we use the dot ( . ) operator, also called period or member access operator. Its associativity is from left to right.


The syntax for accessing a structure member is: 

struct_variable_name.member_name;

 

Example:

std1.name, std1.roll_no, std1.age, std1.fees



Initializing Structure Variables:

Structure members cannot be initialized with declaration because when a structure is declared, no memory is allocated to it. Memory is allocated only when structure variables are declared. 

There are two ways to initialize structure variable: all the fields in one statement or one field at a time. To initialize all fields in one statement, we declare variable as usual, but assign it a list of variables enclosed in curly brackets:


Syntax:     variable={value1, value2, …..};

 

Example: std1={“John”,1,20,1500.00};

Here, “John” is assigned to std1.name, 1 is assigned std1.roll, 20 is assigned to std1.age and 1500.00 is assigned to std1.fees

To reference an individual field, we use the dot operator to separate the struct variable name and the field name. We can use the dot operator to initialize each field individually:


Example: 

std1.name= “John”;

std1.roll_no=1;

std1.fees=2000;



Advantages of structure:

Here are advantages/benefits for using structure:


  • Structures gather more than one piece of data about the same subject together in the same place.
  • It is helpful when you want to gather the data of similar data types and parameters like first name, last name, etc.
  • It is very easy to maintain as we can represent the whole record by using a single name.
  • Structure allows us to create user defined data-type which can store items with different data types. 
  • In structure, we can pass complete set of records to any function using a single parameter.
  • You can use an array of structure to store more records with similar types.
  • Structure in C eliminates lots of burdens while dealing with records which contain heterogeneous data items, thereby increasing productivity. 
  • Code readability is crucial for larger projects. Using structure code looks user friendly which in turn helps to maintain the project.


  


Post a Comment

0 Comments