Structures in C Programming

Structures in C Programming :

1.	Structure
2.	Array in Structures
3.	Structure with Array
4.	Structures within Structures (Nested Structures)

Structure in C:
Structure is user defined data type which is used to store heterogeneous data under unique name. Keyword ‘struct’ is used to declare structure.
The variables which are declared inside the structure are called as ‘members of structure’.

Syntax:

struct structure_nm
{
	<data-type> element 1;
	<data-type> element 2;
	- - - - - - - - - - -
	- - - - - - - - - - -
	<data-type> element n;
}struct_var;

Example :

struct emp_info
{
	char emp_id[10];
	char nm[100];
	float sal;
}emp;

Note :
1. Structure is always terminated with semicolon (;).
2. Structure name as emp_info can be later used to declare structure variables of its type in a program.

* INSTANCES OF STRUCTURE IN C:
Instances of structure can be created in two ways as,
Instance 1:

struct emp_info
{
	char emp_id[10];
	char nm[100];
	float sal;
}emp;

Instance 2:

struct emp_info
{
	char emp_id[10];
	char nm[100];
	float sal;
};
struct emp_info emp;

In above example, emp_info is a simple structure which consists of structure members as Employee ID(emp_id), Employee Name(nm), Employee Salary(sal).

* ACCESSING STRUCTURE MEMBERS IN C:
Structure members can be accessed using member operator ‘.’ . It is also called as ‘dot operator’ or ‘period operator’.
structure_var.member;

Program :

/*  Program to demonstrate structure.
Creation Date : 23 Nov 2010 02:41:01 AM
Author : www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>

struct comp_info
{
	char nm[100];
	char addr[100];
}info;

int main(void)
{
	clrscr();
	printf("\n Enter Company Name : ");
	gets(info.nm);
	printf("\n Enter Address : ");
	gets(info.addr);
	printf("\n\n Company Name : %s",info.nm);
	printf("\n\n Address : %s",info.addr);
	getch();
	return 0;
}

Output :

 Enter Company Name : TechnoExam, Technowell Web Solutions
 Enter Address : Sangli, Maharashtra, INDIA
 
 Company Name : TechnoExam, Technowell Web Solutions
 Address : Sangli, Maharashtra, INDIA_

Structure With Array in C:
We can create structures with array for ease of operations in case of getting multiple same fields.
Program :

/*  Program to demonstrate Structure With Array.
Creation Date : 23 Nov 2010 06:49:00 PM
Author : www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>

struct emp_info
{
	int emp_id;
	char nm[50];
}emp[2];

int main(void)
{
	int i;
	clrscr();
	for(i=0;i<2;i++)
	{
		printf("\n\n\t Enter Employee ID : ");
		scanf("%d",&emp[i].emp_id);
		printf("\n\n\t Employee Name : ");
		scanf("%s",emp[i].nm);
	}
	for(i=0;i<2;i++)
	{
	   printf("\n\t Employee ID : %d",emp[i].emp_id);
	   printf("\n\t Employee Name : %s",emp[i].nm);
	}
	getch();
	return 0;
}

Output :

	Enter Employee ID : 1
	
	Employee Name : ABC
	
	Enter Employee ID : 2
	
	Employee Name : XYZ
	
	Employee ID : 1
	Employee Name : ABC
	Employee ID : 2
	Employee Name : XYZ_

Leave A Comment

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