STRUCTURES
As we have seen earlier ,in C we can store similar kind of data items under a same common name in continous memory locations using arrays. But in most of the cases we need to store elements of different data types. For example if we want to store the information of student such as his name, class, roll number, we cannot store them under a single common name.
For this purpose, C allows the concept of structures. Structure
can be treated as a programmer-defined data type. Arrays are
built in data types. They can declared and used any time. But in
case of structures, we have to design the structure with
required fields and then declare variables for it.
Structure
Definition: The general format of structure definition is
as follows….
struct tag_name { member 1; member 2; ------------- -------------- };
- The structure definition contains the key word "struct", followed by tag name which is nothing but the structure name.
- Each member is declared with its own data type and name as usually. These fields are called structure members or Variables.
- The entire structure is terminated with a semi colon.
Note: Structures can be declared inside main( ) or
outside it. Declaring the structure outside main( ) makes it
global which can be used by other functions.
Example:
We declare here a structure, containing the name, class, roll
number of a student.
struct student { char sname[20]; int class; int sno; };
This statement declares a new data type called ‘struct student’.
The above structure defines a student’s information which can be
accessed only after declaring structure variables.
Declaring structure Variables
The structure variable declaration is similar to that of
variables of any other data type.
It includes the following
elements
- The keyword struct
- The structure tag name
- Variable name(s).
- Semi colon.
Suppose we declare here a employee structure and then variables for it as follows.
struct employee { Char ename[20]; int eno; /* Structure definition */ float esal; };
The structure variables can be declared as
struct employee emp; /* Declaration of structure variables*/
Therefore the general format for declaring structure variables is
struct tag_name variable_name;
The structure variables can also be declared generally as follows
struct tag_name { Member 1; ------------- ------------- }variable name;
Note:
The structure members themselves are not
variables. They do not occupy any memory untill structure
variables are declared.
Accessing Structure Members
Structure members should be linked with their respective
structure variables in order to access them. This can be done
with the use of a special operator ‘.’ Which is called as member
operator (or) dot operator (or) period operator.
For
example:
struct library { char title[20]; char author[20]; int price; }book1,book2;
Now the structure members can be accessed as follows
book1.title
book1.author
book1.price
Once the dot
operator is used the members can be treated as ordinary
variables and can be read or initialized or any other operations
can be performed.
scanf("%s",book1.title);
scanf("%s",book1.author); scanf("%d",book1.price);
Structure variables can also be initialized as
struct health { int weight; float height; }student={60,180.75}; (OR) struct health student={60,180.75};
Individual members cannot be initialized inside the
structure template.
Write a program to print name,
address using structure.
#include< stdio.h> #include< conio.h> void main( ) { struct details { char name[20]; int hno; char street[20],area[20],city[20]; }obj; clrscr( ); printf("\nEnter name"); gets(obj.name); printf("\n Enter house number:"); scanf("%d",&obj.hno); printf("\n Enter street, area and city:"); scanf(%s%s%s",obj.street,obj.area,obj.city); clrscr( ); printf("\n Name : %s",obj.name); printf("\n House Number : %d",obj.hno); printf("\n Street Name : %s",obj.street); printf("\n Area : %s",obj.area); printf("\n City : %s",obj.city); getch( ); }
Define a structure ABS that contains age, designation,
and salary. Using this structure write a C program to read this
information for one person and print it.
#include< stdio.h> #include< conio.h> struct ABS { char name[20]; int age; char desig[20]; /* designation */ float salary; }s; void main() { clrscr(); printf("\n Enter name:"); gets(s.name); printf("\n Enter age:"); scanf("%d",&s.age); printf("\n Enter designation:"); scanf("%s",s.desig); printf("\n Enter salary :"); scanf("%f",&s.salary); clrscr(); printf("\n Name : %s",s.name); printf("\n Age : %d",s.age); printf("\n Designation : %s",s.desig); printf("\n Salary : %f",s.salary); getch(); }