You are on page 1of 18

COMPUTER PROGRAMMING (15A05101) GIST

STRUCTURES
A structure is a collection of data items of different data types or same data type that are
referenced under one name. The structure concept provides a convenient way of keeping related
information together. The data items that form the structure are called members (Also called
elements or fields). The individual elements can be accessed and processed separately. We can
define our own data type by using structures.
Declaring a Structure:
The keyword ‘struct’ is used to define a structure. The structure definition is as follows:
Syntax:
Struct tag
{
datatype member1;
datatype member2;
:
:
datatype member-n;
} var1, var2, …, var-n ;
In the above syntax,
 The tag is the name of the structure
 Member1, member2, …, member-n are the individual members of the structures. These are
nothing but variables.
 Var1, var2, …, var-n are the structure type variables.
Example:
Struct stud
{
int htno;
char name[30];
int m1, m2;
float avg;
} st1, st2;
The above example defines a structure called ‘stud’ and declares variables st1 and st2 of that
type.
Structure variables can also be declared separately as follows:
struct stud st1, st2;
In the above examples, for each variable i.e. st1 or st2, the conceptual view of memory
representation is as shown below:

2 bytes 30 bytes 2 bytes 2 bytes 4 bytes

St1.htno St1.name St1.m1 St1.m2 St1.avg


Accessing Structure Elements:
Individual members of a structure are accessed through the use of dot (.) operator. This
operator is placed between a structure type variable and the field being used.
Syntax: Structurevariable.membername

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


1
COMPUTER PROGRAMMING (15A05101) GIST

For example, to access the members of ‘st’ variable declared in the above example are as follows:
St.htno accesses the member htno of st variable
St.name accesses the member name of st variable.
Initializing structure type variables:
Like simple and array variables, structure type variables can also be initialised while they are
declaring.
Syntax: struct tag var1={values}, var2={values}, ……, var-n = {values};

Here, Var1, var2, …, var-n are the structure type variables. The values for the structure variables
must be enclosed within curly braces. The values must be separated by commas. The number and
order of values must be same to the number and order of members defined in the tag.
Example:
Struct stud st1={1,”ABC”,45,55}, st2={2,”XYZ”,90,89};

Structure Assignments:
The information contained in one structure can be assigned to another structure of the same
type using assignment operator. We do not need to assign the values of each member separately.
Syntax:
Structurevariable1 = structurevariable2 ;
Example:
Struct temp
{ int a,b;
};
main( )
{
struct temp m,n;
printf(“Enter 2 values :“);
scanf(“%d %d”,&m.a, &m.b);
n = m; /* structure assignment */
printf(“a=%d , b=%d “,n.a, n.b);
}
OUTPUT: Enter 2 values:10 20
a=10 b=20
ARRAY OF STRUCTURES
Like any other type, we can also declare an array of structures. To declare an array of
structures, we must first define a structure and then declare an array of that type.
Syntax: Struct tag arrayname[size];
Example:
Struct student
{ int htno;
char name[20];
float avg;
};
struct student st[20];

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


2
COMPUTER PROGRAMMING (15A05101) GIST

The above example creates 20 elements of type ‘student’. The conceptual view of the above
declaration is as shown below:

htno name avg


St[0]
St[1]
St[2]
:
:
:
St[19]

To access an individual element in an array of structures, we use a subscript or index. Like all
array variables, arrays of structures also begin indexing at 0. The individual members are accessed as
follows:
St[0].htno, st[2].name, st[19].avg , …….
Program:-
Struct student
{
int sno;
char sname[20];
float marks;
};
main()
{
Struct student s[10];
int i;
clrscr();
for(i=0;i<2;i++)
{ printf(“Enter details of student %d”,i+1);
Scanf(“%d%s%f”,&s[i],sno,s[i].sname,&s[i].marks);
}
for(i=0;i<2;i++)
{ printf(“the details of student %d are”,i+1);
Printf(“Number=%d”,s[i].sno);
Printf(“Name=%s”,s[i].sname);
Printf(“Marks=%f”,s[i].marks);
}
getch(); }
Output:-
Enter the details of student 1: 100 Nagendra 99
Enter the details of student 2: 200 Rajesh 80
The details of student 1 are: Number=100 Name=Nagendra Marks=99
The details of student 2 are: Number=200 Name=Rajesh Marks=80

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


3
COMPUTER PROGRAMMING (15A05101) GIST

Differences between Structures and Arrays:


Arrays Structures
1. An array is a group of elements of same type 1. A structure is a collection of data items may
that share a common name of different types referenced under one
name

2. An array type variable can be defined using 2. A structure can be defined by using the
square brackets. keyword ‘struct’
Example: int a[20]; Example:
Struct stud
{
int htno;
char nm[5];
float avg;
} st;
3. The individual elements are accessed by 3. The individual members of the structure are
writing a value in square brackets after array accessed through the use of dot operator.
name. Example:
Example: st.htno refers to htno of st
A[0] represents first element st.nm refers to nm of st
A[3] represents 4th element

4. The conceptual view of the above array in the 4. The conceptual view for the above structure
memory is is

a[0] a[1] a[2] a[19] St.htno st.nm st.avg

5. Arrays can be initialised while they are 5. The structure members are initialised as
declaring as follows follows
int a[5]={1,3,5,6,2}; Struct stud st={1,”ABC”,75.25};

6. The direct assignment is not possible 6. Variables of same type structures can
between two array variables assigned directly
int a[5]={1,2,3,4,5}, b[5]; Struct stud st1={1,”ABC”,75}, st2;
b = a; is invalid assignment st2 = st1; is valid assignment

7. If we access the name of the array, it returns 7. If we access name of the structure, it returns
base address of the array the value of first element in the structure

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


4
COMPUTER PROGRAMMING (15A05101) GIST

STRUCTURE WITH IN STRUCTURE


Creating a structure inside another structure is called ‘Nested structure’. It is also known as
‘Embedded structure’. The structure variables can be a normal structure variable or a pointer
variable to access the data. Consider the following example:
Struct emp
{
int eno;
char ename[20];
float sal;
float da;
float hra;
float cs;
}e;
This structure defines eno, ename , sal and three kinds of allowances can be grouped
together and declared under a sub-structure as shown below.
Struct emp
{
int eno;
char ename[20];
float sal;
Struct allowances
{
float da;
float hra;
float ca;
}a;
}e;

The inner most member in a nested structure can be accessed by chaining all the concerned
structure variables(from outer most to inner mst)with the member using dot operator.
Example program
Write a program for employee details structure within structure
/* Employee details structure within structure*/

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


5
COMPUTER PROGRAMMING (15A05101) GIST
#include<stdio.h>
#include<conio.h>
main()
{ struct employee
{ int empno;
char empname[50];
float empsal;
struct date
{ int day;
int month;
int year;
} dob;
}; struct employee e1;
clrscr();
/* reading a structure within structure */
printf("Enter empno,empname,empsal &empdob:");
scanf("%d%s%f",&e1.empno,e1.empname,&e1.empsal);
scanf("%d%d%d",&e1.dob.day,&e1.dob.month,&e1.dob.year);
/* print a structure within structure */
printf("Employee details:");
printf("\nemployee number:%d",e1.empno);
printf("\nemployee name:%s",e1.empname);
printf("\nemployee salary:%f",e1.empsal);
printf("\ndate of birth:%d/%d/%d",e1.dob.day,e1.dob.month,e1.dob.year);
}

Output
Enter empno, empname, empsal &empdob:100
KVNagendra
30200.32
2
4
2010
Employee details:
Employee number: 100
Employee name: KVNagendra
Employee salary: 30200.32
Date of birth: 2/4/2010
STRUCTURES AND FUNCTIONS
There are three ways by which the values of structure can be transferred from one function to
another.
i.passing individual member as arguments to function:
Each member is passed as an argument in the function call. They are collected independently in
ordinary variables in function header.

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


6
COMPUTER PROGRAMMING (15A05101) GIST
Ex: struct date
{
int day;
int mon;
int yr;
};
main()
{
struct date d={02,01,2012};
clrscr();
display(d.day,d.mon,d.yr);
getch();
}
display(int a,int b,int c) Output:
{
printf(“Day=%d”,a); Day=2
printf(Month=%d”,b); Month=1
printf(Year=%d”,c); Year=2012
}

ii.Passing entire structure as an argument to function:-


Name of the structure variable is given as argument in function call. It is collected in another
structure variable in function header.
Disadvantage:- A copy of the entire structure is created again wasting of memory.
Program:
struct date
{
int day;
int mon;
int yr;
};
main()
{
struct date d={02,01,2012};
clrscr();
display(d);
getch();
}
display(struct date dt) Output:-
{
printf(“Day=%d”,dt.day); Day=2
printf(Month=%d”,dt.mon); Month=1
printf(Year=%d”,dt.yr); Year=2012
}

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


7
COMPUTER PROGRAMMING (15A05101) GIST
iii.Passing address of the structure as an argument to the function:
the address of the structure is given in the function call. It is collected in a pointer to structure in
function header.
Advantage:
 No wastage of memory as there is no need of creating a copy again.
 No need of returning the values back as the function can access individually the entire
structure and work on it.
Program:-
struct date
{
int day;
int mon;
int yr;
};
main()
{
struct date d={02,01,2012};
clrscr();
display(&d);
getch();
}
display(struct date *dt) Output:
{
printf(“Day=%d”,dt->day); Day=2
printf(Month=%d”,dt->mon); Month=1
printf(Year=%d”,dt->yr); Year=2012
}

STRUCTURES AND POINTERS


The beginning address of a structure can be accessed by using address operator (&). Thus if
‘variable’ represents structure type variable then ‘&variable’ represents the starting address of that
variable. The pointer variable of structure type can be declared as follows:
Type *ptvar;
The ‘type’ is structure datatype and ‘ptvar’ represents the name of pointer variable.
Example:
Struct account
{
int acno;
char acname[30];
float bal;
};
struct account cust,*cptr;
In this example, ‘cust’ is a structure variable of type ‘account’ and ‘cptr’ is a pointer variable
that points to structure variable of type ‘account’.

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


8
COMPUTER PROGRAMMING (15A05101) GIST
To assign the beginning address of the structure variable ‘cust’ to the pointer variable ‘cptr’ is
as shown below:
cptr = &cust;
The individual structure members can be accessed using pointer variable with the operator
arrow () as ptvar  member. And the arrow () operator can be combined with dot operator to
access a sub-member within the structure.
For example,
1. To access acno member of cust variable using pointer, we can refer that member as
cptracno;
2. To access first character of member ‘cname’, the pointer can be used as cptrcname[0];

Note:
If cptr = &cust; then cust.acno, cptracno, (*cptr).acno are all same
Program:-
Struct student
{
int sno;
Char sname[20];
Float marks;();

};
Main()
{
struct student s;
struct student *st;
clrscr();
printf(“\n Enter SNO,Sname,Marks:”); OUTPUT:-
scanf(“%d%s%f”,&s.sno,s,sname,&s.marks”); Enter Sno,Sname,Marks:
st=&s; 10,nagendra,99
printf(“DETAILS OF THE STUDENT ARE:”); DETAILS OF THE STUDENT ARE:
printf(“Number=%d”,st->sno); Number=10
printf(“name=%s”,st->sname); Name=Nagendra
printf(“Marks=%f”,st->marks); Marks=99
getch();
}
Uses of pointers to structures:
There are two primary uses of structure pointers. They are
1. To pass a structure to a function using call-by-reference
2. To create a dynamic data structure like linked list.

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


9
COMPUTER PROGRAMMING (15A05101) GIST
UNIONS
Like structures, a union is also a collection of data items may be of different types or same data
types referenced under one name.
The members within a union all share the same storage area within the computer’s memory. Unions
are useful for the applications involving multiple members where values need not be assigned to all of the
members at one time.
When a union is declared, the compiler automatically allocates memory location to hold largest data
type of members in the union.
Declaring a Union:
The keyword ‘union’ is used to define a union. The union definition is as follows:
Syntax:
union tag
{
datatype member1;
datatype member2;
:
:
datatype member-n;
} var1, var2, …, var-n ;
In the above syntax,
 The tag is the name of the union
 Member1, member2, …, member-n are the individual members of the union. These are nothing but
variables.
 Var1, var2, …, var-n are the union type variables.
Example:
Union temp
{ int a;
float b;
char c;
} m, n;
The above example defines a union called ‘temp’ and declares variables ‘m’ and ‘n’ of that type.
Union variables can also be declared separately as follows:
union temp m, n;
In the above examples, the union variables occupy 4 bytes of memory. Because the largest member in the
above example is ‘b’ of float data type.
The memory representation for the above declaration is as shown below:
m

m.a
m.b
m.c
In the above example, all the members i.e. a, b and c share same storage area i.e. 4 bytes of memory.
Accessing Union Members:
We can access the individual members of a union through the use of a dot operator. The general
format is as follows:
Unionvariable.member;
Example: m.a, m.b, ……

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


10
COMPUTER PROGRAMMING (15A05101) GIST
Differences between Structures and Unions:
Structures Unions
1. Structure is a collection of data item may of 1. Union is also collection of data item may of different
different data types referenced under one data types referenced under one name
name

2. Structure is defined by ‘struct’ keyword as 2. The union is defined by ‘union’ keyword as


Struct tag union tag
{ {
datatype member1; datatype member1;
datatype member1; datatype member1;
: :
datatype member-n; datatype member-n;
} var1, var2, …, var-n; } var1, var2, …, var-n;

3. In a structure, memory is reserved for all 3. In a union, memory is reserved only for the largest
members of that structure member of that union
4. All members of structure cannot share the 4. All members of structure can share the same storage
same storage area area
5. All members of structure can be accessed at 5. All members of union cannot be accessed at a time
a time
6. We can store data for all members in the 6. We can store data for only one member of an union
structure

7. The structure variables can be initialised and 7. The union variables cannot be initialised. But if we
can give values for all members want to initialise, we can give value to the first
Example: member only
Struct temp m={10, 50.25, ‘M’}; Example:
Union temp m={10};

UNION OF STRUCTURES:-
A structure can be nested inside a union and it is called union of structures.It is also possible to create a struct
union inside a structure.
Program:-
Struct x
{ int a;
float b;
};
union
Union z
{
Struct x s;
}; a,b,c
Main()
{
Union z u;
Clrscr();

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


11
COMPUTER PROGRAMMING (15A05101) GIST
u.s.a=10;
u.s.b=30.5;
printf(“a=%d”,u.s.a);
printf(b=%f”,u.s.b);
getch();
}
OUTPUT:-
A=10
B=30.5
Typedef Statement:
The typedef feature allows user to define new datatype name that is equivalent to existing
data type. Once a user defined data type has been defined then variables are declared using this new
data type.
The general format for defining a new data type is as follows:

Syntax: typedef type newtype


In the above syntax, ‘type’ refers to an existing data type and ‘newtype’ refers to the new
user-defined data type.

Ex-1:
Typedef int integer;
In the above example, ‘integer’ is user-defined data type which is equivalent to type ‘int’. hence, the
variable declaration,
integer a,b; and
int a,b; are same.

Ex-2:
Struct stud
{ int rno;
char nm[20];
};
typedef struct stud student;

The above example defines a new data type ‘student’ which is equivalent to the existing user-defined
data type ‘struct stud’. Hence the following declarations
Struct stud st1; and student st1; are same.

Program:-
main()
{
typedef int hours;
hours h;
clrscr();
printf(“Enter hours:”);
scanf(“%d”,&h);
printf(“minutes=%d”,h*60);

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


12
COMPUTER PROGRAMMING (15A05101) GIST
printf(“\n seconds=,h*60*60);
getch();
}
OUTPUT:-
Enter hours: 1
Minutes:60
Seconds:3600.
ENUMERATION DATA TYPES
An enumeration data type is a derived (user-defined) data type which is similar to structures and
unions. Its members are constants. These constants represent values and they can be assigned to
corresponding enumerated variables.
Enumeration types are user-defined types that allows programmer to name a finite set of elements
which are called enumerators or enumeration constants or enumeration members.
Defining a Enumeration type:
The general format for defining enumeration type is as follows:
Syntax: enum tag {id1, id2, id3, ……, id-n};
Here, ‘tag’ is the name of enumeration type and id1, id2, id3, ……., id-n are identifiers which represent
enumeration constants. By default, ‘c’ compiler assigns integer values starting from zero to each identifier
automatically. But we can change these values.
Ex-1: enum grade {distinction, first, second, third} ;
In this example, ‘grade’ is the name of enumeration tag, and ‘distinction, first, second, third’ are the
enumeration constants which assigns values 0, 1, 2, 3 respectively.
Ex-2: enum grade {distinction=10, first=20, second=30, third=40} ;
In this example, enumeration constants are assigned with values 10, 20, 30 and 40 respectively.
Note:
1. An enumeration constant cannot be read from the keyboard.
2. An enumeration constant can be assigned to any integer variable.
3. An enumeration constant can also be printed.
Declaring Enumeration Variables:
Once enumeration type has been defined, one or more variables of that type can be declared. The
declaration is as follows:
Syntax: enum tag var1, var2, ..., var-n ;
Every variable of enumeration type occupies only two bytes of memory.
Ex: enum grade g;
program:
main()
{
enum week {mon,tue,wed,thur,fri,sat,sun};
clrscr();
printf(“\n Monday=%d”,mon);
printf(“\n Thursday=%d”,thur);
printf(“\n SaturDay=%d”,sat);
}
Output:-
Mon day=0
Thurs Day=3
Saturday=6

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


13
COMPUTER PROGRAMMING (15A05101) GIST

Advantages:-
1.It Prevents the assignment of invalid value to variables.
Ex:-enum divison{first,second,third,fail}result;
Now we can assign only one of the above enumerators to the variable result, otherwise it gives an error.
2.This data type occupies only two bytes of memory.
3.The use of enumeration variables with in a program can increase the logical clarity of the program.
Disadvantages:-
1.The enumeration constants cannot be read from the key board by using scanf() statement .
2.When we are printing these variables , only the integer values associated with it will be printed on the
screen.
BITFIELDS
These are used to change the order of allocation of memory from bytes to bits. A bit field is a
set of adjacent bits whose size can be from 1 to 16 bits length. There are occasions where data items
require much less than 16 bits of space. In such cases memory will be wasted. Bit fields can pack
several data items in a word of memory.
Syntax:
Datatype name:bit_length;

---the data type can be either int or unsignedint or signed int.


---Bit length specifies the no. of bits.
---The largest value that can be stored in 2n-1. Where ‘n’ is bit length.
Note:
 Bit fields can’t be arrayed.
 scanf() can’t be used to read values into bit fields.
 can not use pointer to access the bit field.
 bit fields should be assigned values with in the range of their size.

Bit Length Range of Values


1 0 to 1
2 0 to 3 (22-1)
3 0 to 7 (23-1)
. .
. .
. .
. .
. .
n 0 to 2n-1
Ex:
1. Struct pack
{
Int count;
Unsigned a:2;
Unsigned b:3;
};
Here count will be in 2 bytes.’a’ and ‘b’ will be packed into next word.

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


14
COMPUTER PROGRAMMING (15A05101) GIST
2. Struct pack
{
Unsigned a:2;
Int count;
Unsigned b:3;
};
Here ‘a’ will be in 2 bytes,’count’ in 2 bytes and ‘b’ in 2 bytes.
Note:-
1. Bitfields are packed into words as they appear in the definition.
2. All unsigned bit fields must be placed in order for effectively using memory.

Program:-
Struct vehicle
{
Unsigned type:3;
Unsigned fuel:2;
Unsigned model:3;
};
main()
{
Struct vehicle v;
v.type=4;
v.fuel=2;
v.model=5;
printf(“type of vehicle =%d”,v.type);
printf(“Fuel=%d”,v.fuel);
printf(“\n model =%d”,v.model);
}
Note: instead of 6 bytes only 2 bytes of memory will be allocated.
QUESTIONS:
1.Define structure and write the general format for declaring ,initializing and accessing member?
2.What is ment by Structure with in structure? give an example?
3.Explain different ways of passing structures as arguments in functions?
4.How structure elements are accessed using pointers ?Which operator is used? Give example?
5.list out the difference between structure and union?
6.Explain the differences between Array and structure?
7.Explain the features of bit field in C language?
8.when are array of structures used? Declare a variable as array of structures and initialize it?
9.what is the general format of a union? Declare a union and assign values to it? Explain the process
of accessing the union members?
10.Explain briefly about type def?
11.what is bit-fields? What are its advantages?
12.what do you mean by enumerated types? Explain with an example?
13.Define Structure and write the general format for declaring and accessing members? (or)
Explain with an example how a structure can be organized in the ‘C’ language? (or)

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


15
COMPUTER PROGRAMMING (15A05101) GIST
What is the use of a structure? Give an example for structure with initialized values? (or)
What is a structure? How is it declared? How it is initialized? (or)
What is the use of struct keyword? Explain the use of dot operator. Give an example for each?
14. Describe nested structure. Draw diagrams to explain nested structure? (or)
What is structure within structure? Give an example for it?
15. When are array of structures used? Declare a variable as array of structures and
initialize it?
16. Distinguish between an array of structures and an array within a structure. Give an example
each?
17. How to compare structure variables? Give an example? (or)
Write a C program to illustrate the comparison of structure variables?
18. Explain the different ways of passing structures as arguments in functions. Write a C program to
illustrate the method of sending an entire structure as parameter to a function?
19.How are structure elements accessed using pointers? Which operator is used? Give an example?
20. Different between a structure and union with respective allocation of memory by the compiler?
Give an example of each?
21.Define a structure type struct ABS that contains name, age, designation and salary . Using this
structure write a C program to read this information for one person from the keyboard and print the
same on the screen.
22. Write a C program to print maximum marks in each subject along with the student name by using
structures. Take 3 subjects and 3 students records?
23. Write a C program to calculate student-wise total for three students using array of structures.
------------------------------------------------------------------------------------------------------------
1. How are structure elements stored in memory?
Whatever be the elements of structure, they are always stored in contiguous memory
locations. The following program would illustrate this

#include<stdio.h>
#include<conio.h>
Main()
{
Struct book
{char name;
Float price;
Int pages;
};
Struct book b1={“B”,13.00,550}
Printf(“\n Address of name=%u”,&b1.name);
Printf(“\n Address of price=%u”,&b1.price);
Printf(“\n Address of pages=%u”,&b1.pages);
}

The output of this program is


Address of name=65518
Address of price=65519

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


16
COMPUTER PROGRAMMING (15A05101) GIST
Address of pages=65523
The structure elements are stored in memory as shown in the figure below

“B” 130.00 550

2.How to copy one structure to another structure of a same data type? Give an example?
C does not support the concept of copying the elements of one array to another just by using
an assignment operator (i.e., by equating the two arrays ) for copying array, it is necessary to copy
the content of array element by element. However, C has the ability to copy the content of every
structure element of one variable into the corresponding elements of another structure variable.
The values associated with obe structure variable can be equated to another variable of same data
type using an assignment operator. While copying the content of a structure, all structure elements
are copied at once(i.e., they are not copied element-by-element). This is because the structure
elements are stored in contiguous memory location. Usage os assignment operator avoids the
programmers from using the concept of piecemeal copying wherein content of structure are copied
element-by-element.

#include<stdio.h>
#include<string.h>
Void main()
{
Struct student
{
Char name[10];
Int mark;
Float average;
};
Struct student s1={“XYZ”,500,100};
Struct student s2,s3;
/*piecemeal copying*/
Strcpy(s2.name,s1.name);
S2.mark=s1.mark;
S2.average=s1.average;
/*copying all elements at once*/
S3=s2;
Printf(“\n%s %d %f”, s1.name,s1.marks,s1.average);
Printf(“\n%s %d %f”, s2.name,s2.marks,s2.average);
Printf(“\n%s %d %f”, s3.name,s3.marks,s3.average);
}
3.Explain the advantages of structure type over the array variable?
The advantages of structure type over the array variable are.
1. Using structures, we can group items of different types within a single entity, which is not
possible with arrays, as array stores similar elements.

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


17
COMPUTER PROGRAMMING (15A05101) GIST
2. The position of a particular structure type variable within a group is not a needed in order to
access it, whereas the position of an array member in the group is required, in order to refer
to it.
3. In order to store the data about a particular entity such a ‘BOOK’, using an array type, we
need three arrays, one for storing the ‘name’, another for storing the ‘price’, and a third one
for storing the ‘number of pages’ etc., hence, the overhead is high. This overhead can be
reduced by using structure type variable.
4. Once a new structure has been defined, one or more variables can be declared to be of that
type.
5. A structure type variable can be used as a normal variable for accepting the user’s input, for
displaying the output etc.
6. The assignment of none ‘struct’ variable to another, reduces the burden of the programmer
in filling the variable’s fields again and again.
7. It is possible to initialize some or all fields of a structure variable at once, when it is declared
8. Structure type allows the efficient insertion and deletion of elements but arrays cause the
inefficiency.
9. For random array accessing, large hash are needed. Hence, large storage space and costs are
required.
10. When a structure variable is created, all of the member variables are created automatically
and are grouped under the given variable’s name.
Similarities Between Structures and Unions
 Both of them facilitates the programmer to declare a program segment holding data of
different types(say int, float,…..etc)
 Syntactically structures and unions are equal

Prepared By: K.V.Nagendra,M.Tech,(Ph.D)


18

You might also like