Welcome Guest   Login   Mar 28, 2024
Tutorials
C
HTML
Java
<?php include("../../config.inc.php"); $section="Tutorials - Programming in C - Chapt 8"; include("../../title.inc.php"); ?>
Chapter-8
Structures

If all the data elements of a program are of the same type, they can be represented as an arrya. If the elements are of different types, however, the array is inadequate to the task. We resort to an entity known in C as structures.

One employees information can be stored in different variables in following fashion.

int empno;
char name[20];
float sal;
 

This can be grouped together in structure and can be stored as single entity.
 
struct empinfo {

int empno;

char name[20];

float sal;

};

A structure defination is specified by the keyword struct. This is followed by templete, surrounded by braces, which describes the members of the structure. A member of a strucutre is a single unit, so the structure shown here has three members.
 

A program to accept structure data information and display the same can be like this.
 
struct empinfo {
int empno;
char name[20];
float sal;
};

main()
{
struct empinfo data;
printf("\nEnter the emp no : ");
scanf("%d",&data.empno);
printf("\nEnter the name : ");
scanf("%s",data.name);
printf("\nEnter the salary :");
scanf("%f",&data.sal);
printf("\nemp no : ",data.empno);
printf("\nname : ",data.name);
printf("\nsalary :",data.sal);
}

Each member of structure variable is specified by following the variable name with a period and the member name. The period is the structure member operator.
 
 

Assigning the structure values at the time of declaration only.
 
main()
{

  struct empinfo data={23,"Deepak",2300.50);
 
  printf("\nemp no : ",data.empno);

  printf("\nname : ",data.name);

  printf("\nsalary :",data.sal);
}


 

My New Blog

My Award winning Whitepaper

Contradicting Light
My Personal Blog


Trace Mobile Location



My Book Reviews
 




Tech Jokes Worth Reading
 
Top
Home
www.deepjava.com 1999-2017 (C) Developed and maintained by D-Kay Consultancy