Published on: 12th Nov 2010    |   Last Updated on: 24th Nov 2011

Contents :

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

Structure :

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 :

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 stucture members as Employee ID(emp_id), Employee Name(nm), Employee Salary(sal).

* Aceessing Structure Members :

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; void main() { 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(); }

Output :


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

Link this post on your Blog/Website :

Follow us on :   Technoexam on facebook Technoexam on twitter Technoexam on linkedin Technoexam on youtube