Professional Documents
Culture Documents
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:
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];
The above example creates 20 elements of type ‘student’. The conceptual view of the above
declaration is as shown below:
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
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
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
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*/
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.
Note:
If cptr = &cust; then cust.acno, cptracno, (*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.
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, ……
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();
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);
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;
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)
#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);
}
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.