Structure: Structure is a derived data type which is derived from primary data types such as int, char and float etc.It is also known as composite data type that can be used to define collection of different or similar data type under one name in a particular block of computer memory.
Another way we can define the structure as a single entity holding variables of different data types that are related to each other. The function defined ouside the structure can access all data member inside the structure.
To access the data members of structure in the main function, we need to create a structure variable.
Introduction to Structure in C Programming.
Let us assume that we are going to develope a software for a company y to manage all employees information.
Employees has employee number,employee name, employee salary, employee job,employee department number
and so on.
we won't be able to use an array to store all information of the employees given above.
As we discussed earlier tutorials, an array is contiguous memory allocation used to store only similar data type.
From the given fig. problem statement we need to store a employees information in single entity or variable.
The employee has different type of attributes having differnt data types like integer ,character,float etc.
As the programmer you wish to store this information using C program.
If you are thinking array as the solution it will be very difficult to manage n number of arrays.
Where n is total number of array of every attribute of employee e.g name,salary,deptno.
What is the solution for the given above problem?
C language already has the the solution for the same problem.That is structure it is also called the heterogeneous data type.
Using structure we can store the given employee informtion ,
we can store different data type attributes of employees.
Advantage of Structure in C programming.
Use of structure has several advantages in c programming.
In this article we are going to list all the key advantages.
a. Reduced complexity: Let think of storing employee records of seven different employees with employee number, name,job, salary, bonus and department number like in above example without structure!
What do we need here?
seven integer type variables for storing employee number of seven employees, seven string variables for storing name of seven employees, seven long int type variables for storing salary, seven string variables for storing job of seven employees and seven float type of variable for storing bonus. It's Terrible! right!. Don’t do it that way! .The solution for the problem is structure. That’s it!.
Here is simple example for doing that:
struct employee
{
int empno;
char ename[30];
char job[30];
int salary;
float bonus;
int deptno;
};
struct employee e[7];
b. Heterogeneous collection of data items: Structure can store items with different data types whic allows us to create user defined data-type . For example, if we want to store employees' records with their empno, ename, job,salary,bonus and deptno then it comprises items with different types i.e. empno number can be of integer number type, ename is of string type, job is of string type,salary is of string type and bonus is of floating number type .
In programming C Language, it can be achieved by using structure like this:
struct employee
{
int empno;
char ename[30];
char job[30];
float bonus;
int salary;
int deptno;
};
Here we can create structure variables using struct employee e1, e2 and these variables can store all the details of two employees.
c.Increased productivity: Structure increase the productivity and eliminates lots of burden when dealing with the records which contains heterogeneous data items.
d.Maintainability of code:Using structure Complex types records can be represented by single name.
e.Enhanced code readability: for larger projects readability of the code is crucial. Structure helps the programmer to manage the code very friendly which in turn helps to maintain programs and the project. Using Structure Complex types records can be represented by single name.
f.Mathematical operations: by using primary or basic data-types we can not represent distances (feet-inch system),complex numbers, times (hour-minute-second system). Mathematical operations like subtracting complex numbers, or adding them; finding the difference between two time periods in an hour-minute-second ; adding two distances in a feet-inch systemsystem can be done effectively using the concept of structure.
Here is simple example with complex numbers:
struct complex
{
float real;
float imag;
};
struct complex cmplx1, cmplx2, sum;
Previous Topic:-->> Function Assignments || Next topic:-->>Array Vs Structure.
Other Topics:
Variables and Identifiers
Relational Operators
if-else statements
Switch case
While Loop
Infinite while Loops
C FOR Loop
Infinite for Loops
Continue in Loops
One Dimensional Array
Two Dimensional Arrays
Read and Display 2D Arrays
Types of functions
Passing Array To Functions
Nesting of Function
Array vs Structure
Array of Structure
Structures and Functions
Structures Within Structures
Use Of Pointers In C
File Handling In C
Loops FAQ
Arrays FAQ
count vowels in a file
Function FAQ
Conditional Statements Assignments
For Loops Assignments
Arrays Assignments
Function Assignments
Structure Assignments
Pointers Assignments
Files Assignments
Storage classes Assignments
Binary Files
count words,lines in a file
Copy files
Update File
Continue in Loops
break in Loops
Difference Between While and Do while
difference while do..while & for
malloc
calloc
Storage Classes
Operators MCQ
Conditional Statements MCQ
Loops MCQ
Arrays MCQ
Function MCQ
Structure MCQ
Pointers MCQ
Files MCQ
Storage classes MCQ