A structure is in many ways similar to a record. It stores related information about an entity. Structure is basically a user-defined data type that can store related information (even of different data types) together. The major difference between a structure and an array is that an array can store only information of same data type.
A structure is therefore a collection of variables under a single
name. The variables within a structure are of different data types and each has
a name that is used to select it from the structure.
Structure Declaration
A
structure is declared using the keyword struct followed by the structure name.
All the variables of the structure are declared within the structure. A
structure type is generally declared by using the following syntax:
struct struct_name
{
data_type
var–name;
data_type var–name;
...............
};
Initialization of Structures
A
structure can be initialized in the same way as other data types are
initialized. Initializing a structure means assigning some constants to the
members of the structure. When the user does not explicitly initialize the
structure, then C automatically does it. For int and float members, the values
are initialized to zero, and char and string members are initialized to '\0' by
default. The initializers are enclosed in braces and are separated by commas.
However, care must be taken to ensure that the initializers match their
corresponding types in the structure definition. The general syntax to
initialize a structure variable is as follows:
struct struct_name
{
data_type member_name1;
data_type member_name2;
}struct_var = {constant1, constant2,
constant3,...};
or
struct struct_name
{
data_type member_name1;
data_type member_name2;
};
struct struct_name struct_var = {constant1,
constant2, constant 3,...};
Accessing the Members of a Structure
Each
member of a structure can be used just like a normal variable, but its name
will be a bit longer. A structure member variable is generally accessed using a
'.' (dot) operator. The syntax of accessing a structure or a member of a
structure can be given as:
struct_var.member_name
The dot
operator is used to select a particular member of the structure. For example,
to assign values to the individual data members of the structure variable
studl, we may write
stud1.r_no = 01;
stud1.name = "Rahul";
stud1.course = "BCA";
stud1.fees = 45000;
To input
values for data members of the structure variable stud1, we may write
scanf("%d",
&stud1.r_no);
scanf("%s", stud1.name);
Similarly,
to print the values of structure variable stud1, we may write
printf("%s", stud1.course);
printf("%f",
stud1.fees);
Example of Structure
#include<stdio.h>
#include<conio.h>
struct student{
int roll;
char name[20];
};
void main(){
struct student stud;
clrscr();
printf("Enter roll number and name:");
scanf("%d%s",&stud.roll,stud.name);
printf("\nRoll Number:%d\tName:%s",stud.roll,stud.name);
getch();
}
Output
Enter roll number and name: 1 hanuman
Roll Number: 1 Name: Hanuman
Passing Structure to functions
For
structures to be fully useful, we must have a mechanism to pass them to
functions and return them. A function may access the members of a structure in
two ways:
Call by value
The values
of the variables are passed by the calling function to the called function.
Example of Passing Structure to function (call by value)
#include<stdio.h>
#include<conio.h>
struct student{
int roll;
char name[20];
};
void display(struct student p){
printf("\nRoll Number:%d\tName:%s",p.roll,p.name);
}
void main(){
struct student stud,p;
clrscr();
printf("Enter roll number and name:");
scanf("%d%s",&stud.roll,stud.name);
display(stud);
getch();
}
Output
Enter roll number and name: 1 Hanuman
Roll Number: 1 Name: Hanuman
Call by address
The
addresses of the variables are passed by the calling function to the called
function.
Example of Passing Structure to function (call by address)
#include<stdio.h>
#include<conio.h>
struct student{
int roll;
char name[20];
};
void display(struct student *p){
printf("\nRoll Number:%d\tName:%s",p->roll,p->name);
p->roll=20;
}
void main(){
struct student stud,*p;
clrscr();
printf("Enter roll number and name:");
scanf("%d%s",&stud.roll,stud.name);
display(&stud);
printf("\nRoll Number:%d\tName:%s",stud.roll,stud.name);
getch();
}
Output
Enter roll number and name: 1 Hanuman
Roll Number: 1 Name: Hanuman
Roll Number: 20 Name: Hanuman
Pointer to Structure
Pointer is a variable which points to the address of another
variable of any data type like int , char , float etc. Similarly we can have a
pointer to structures where a pointer variable can point to the address of a
structure variable. For example
struct student stud,*p;
p=&stud;
&p->roll
p->name
Example of Pointer to structure
#include<stdio.h>
#include<conio.h>
struct student{
int roll;
char name[20];
};
void main(){
struct student stud,*p;
clrscr();
p=&stud;
printf("Enter roll number and name:");
scanf("%d%s",&p->roll,p->name);
printf("\nRoll Number:%d\tName:%s",p->roll,p->name);
getch();
}
Output
Enter roll number and name: 1 Hanuman
Roll Number: 1 Name: Hanuman
Structure With in Structure
A
structure can be placed within another structure, i.e., a structure may contain
another structure as its member. A structure that contains another structure as
its member is called a nested structure. Let us now see how we declare
nested structures. Although it is possible to declare a nested structure with
one declaration, it is not recommended. The easier and clearer way is to
declare the structures separately and then group them in the higher level
structure. When you do this, take care to check that nesting must be done from
inside out (from lowest level to the most
inclusive
level), i.e., declare the innermost structure, then the next level structure,
working towards the outer (most inclusive) structure.
struct struct_name
{
Int
roll;
char name[20];
struct Age{
int d;
int m;
int y;
}ag;
}
Example of Structure within Structure
#include<stdio.h>
#include<conio.h>
struct student{
int roll;
char name[20];
struct age{
int d;
int m;
int y;
}ag;
};
void main(){
struct student stud;
clrscr();
printf("Enter student record:");
scanf("%d%s%d%d%d",&stud.roll,stud.name,&stud.ag.d,&stud.ag.m,&stud.ag.y); printf("\nRoll Number:%d\tName:%s\tAge:%d-%d-%d",stud.roll,stud.name,stud.ag.d,stud.ag.m,
stud.ag.y);
getch();
}
Output
Enter student record: 1
Hanuman
22
01
1999
Roll Number: 1 Name: Hanuman Age: 22-01-1999
Structure With in Structure by Pointer
Passing
large structures to functions using the call by value method is very
inefficient. Therefore, it is preferred to pass structures through pointers. It
is possible to create a pointer to almost any type in C, including the
user-defined types. It is extremely common to create pointers to structures. Like
in other cases, a pointer to a structure is never itself a structure, but
merely a variable that holds the address of a structure. The syntax to declare
a pointer to a structure can be given as,
struct struct_name
{
data_type member_name1;
data_type member_name2;
}*ptr;
or
struct struct_name *ptr;
For our
student structure, we can declare a pointer variable by writing
struct
student *ptr_stud, stud;
The next
thing to do is to assign the address of stud to the pointer using the address
operator
(&),
as we would do in case of any other pointer. So to assign the address, we will
write
ptr_stud = &stud;
Example of Structure within Structure
by pointer
#include<stdio.h>
#include<conio.h>
struct student{
int roll;
char name[20];
struct age{
int d;
int m;
int y;
}ag;
};
void main(){
struct student stud,*p;
clrscr();
p=&stud;
printf("Enter student record:");
scanf("%d%s%d%d%d",&p->roll,p->name,&p->ag.d,&p->ag.m,&p->ag.y);
printf("\nRoll Number:%d\tName:%s\tAge:%d-%d-%d",p->roll,p->name,p->ag.d,p->ag.m,
p->ag.y);
getch();
}
Output
Enter student record: 1
Hanuman
22
01
1999
Roll Number: 1 Name: Hanuman Age: 22-01-1999
Array of Structure
An array
of structures is declared in the same way as we declare an array of a built-in
data type. Another example where an array of structures is desirable is in case
of an organization. An organization has a number of employees. So, defining a
separate structure for every employee is not a viable solution. So, here we can
have a common structure definition for all the employees.This can again be done
by declaring an array of structure employee.The general syntax for declaring an
array of structures can be given as,
struct struct_name
{
data_type member_name1;
data_type member_name2;
};
struct struct_name struct_var[index];
Consider
the given structure definition.
struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
A student
array can be declared by writing,
struct student stud[30];
|
#include<stdio.h>
#include<conio.h>
struct student{
int roll;
char name[20];
};
void main(){
struct student stud[5];
int i;
clrscr();
printf("Enter student record:");
for(i=0;i<5;i++){
scanf("%d%s",&stud[i].roll,stud[i].name);
}
for(i=0;i<5;i++){
printf("\nRoll Number:%d\tName:%s",stud[i].roll,stud[i].name);
}
getch();
}
Output
Enter student record: 1 Hanuman
2 Siyag
3 Jaat
4 Choudhary
5 HSJC
Roll Number: 1 Name: Hanuman
Roll Number: 2 Name: Siyag
Roll Number: 3 Name: Jaat
Roll Number: 4 Name: Choudhary
Roll Number: 5 Name: HSJC
Typedef
Typedef is a
keywords used in c language to assign alternative names to existing datatype.
Its mostly used with user defined datatypes, when names of the datatypes become
slighty complicated to use in programs.
Typedef <existing_name>
<alias_name>
Typedef unsigned long ulong;
Typedef struct struct_name{
Type member;
}Type_name;
Example of typedef
#include<stdio.h>
#include<conio.h>
typedef struct student{
int roll;
char name[20];
}stud;
void main(){
stud st;
clrscr();
printf("\nEnter roll number and name:");
scanf("%d%s",&st.roll,st.name);
printf("\nRoll number:%d\tName:%s",
st.roll,st.name);
getch();
}
Output
Enter roll number and name: 1
Hanuman
Roll number: 1 Name: hanuman
Void Pointer
The void pointer In C is a pointer which is not associated with any data types.It points to some data location in the storage means points to the address of variable.It is also called general purpose pointer. In c malloc() and calloc() functions return void * or generic pointers.
Example of void pointer
#include<stdio.h>
#include<conio.h>
void main(){
void *p;
int i=10;
p=&i;
clrscr();
printf("%d",*((int *)p));
getch();
}
Output
10
Malloc
Malloc method in c is used to
dynamically allocate a single block of memory with the specified size.It
returns a pointer of type void which can be cast into a pointer of any form.It
intilializes each block with default garbage value.
A=(int *)malloc(sizeof(int)*n);
Example of malloc
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main(){
int i,n,*a;
clrscr();
printf("Enter value of a:");
scanf("%d",&n);
a=(int *)malloc(sizeof(int)*n);
printf("Enter elements in array:");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\nElements in array:");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
free(a);
getch();
}
Output
Enter value of n: 5
Enter elements in array:1
2
3
4
5
Elements in array: 1 2 3 4 5
Calloc
Calloc method in
c is used to dynamically allocate the specified number of blocks of memory of
the specified type.It initializes each block with a default value ‘0’.
A=(int *)calloc(sizeof(int),n);
Example of calloc
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main(){
int i,n,*a;
clrscr();
printf("Enter value of n:");
scanf("%d",&n);
a=(int *)calloc(sizeof(int),n);
printf("Enter elements in array:");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\nElements in array:");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
free(a);
getch();
}
Output
Enter value of n: 5
Enter elements in array:1
2
3
4
5
Elements in array: 1 2 3 4 5
Union
The concept of union is adopted from structure union follows the same
syntax and way of declaration as followed by structure only one member can
contains a value at a time.
union union_name{
int x;
float y;
char z;
}u;
Example of union
#include<stdio.h>
#include<conio.h>
union student{
int roll;
char name[20];
};
void main(){
union student stud;
clrscr();
printf("Enter roll number:");
scanf("%d",&stud.roll);
printf("\nEnter name:");
scanf("%s",stud.name);
printf("\nRoll number:%d",stud.roll);
printf("\nName:%s",stud.name);
getch();
}
Output
Enter roll number:11
Enter name: hanuman
Roll number: 11
Name: hanuman
0 Comments