+2 AGNI STATE
COMPUTER SCIENCE - CHAPTER 1
Structures and Pointers
STRUCTURES
Structure is a user-defined data type of C++ to represent a collection of logically
related data items, which may be of different types, under a common name.
Arrays allow to define type of variables that can hold several data items of the
same kind. Similarly structure is another user defined data type available in C++
that allows to combine data items of different kinds.
• Structure is a user defined data type
• It is a collection of heterogeneous data items
• It can be defined using the key word STRUCT
DEFINING A STRUCTURE
To define a structure, you must use the struct statement. The struct statement
defines a new data type, with more than one member. The format of the struct
statement is as follows
struct structure tag
{
data_type variable;
data_type variable;
data_type variable;
……………………………..
……………………………..
data_type variable;
} [one or more structure variables];
Eg:-1
Struct student
{
int adm_no;
char name[20];
char group[20];
float fee;
};
Eg-2
Struct date
{
int dd;
int mm;
int yy;
};
Eg-3
Struct strdate
{
int day;
char month[10];
int year;
};
VARIABLE DECLARATION AND MEMORY ALLOCATION
Structure variable can be declared by the syntax
Struct structure-name structure-variable;
struct structure_tag var1, var2, ..., varN;
or
structure_tag var1, var2, ..., varN;
Eg:
Struct student s;
Student s;
date dob, today; OR struct date dob, today;
strdate adm_date, join_date;
MEMORY ALLOCATION OF STRUCTURE VARIABLE
A c++ compiler allocate separate memory location for each and every structure
members.
Total 28 bytes
Roll_no 4 byte
Name 20*1=20 byte
Mark 4 byte
VARIABLE INITIALISATION
structure_tag variable={value1, value2,..., valueN};
Eg:
student s={3452, "Vaishakh", "Science", 270.00};
ACCESSING ELEMENTS OF STRUCTURE
The structure member can be accessed by using the structure variable with dot(.)
operator.
structure_variable.element_name
s.roll_no=5;
s.name=“abc”;
today.dd = 10;
strcpy(adm_date.month, "June");
cin >> s1.adm_no;
cout << c1.real + c2.real;
Q:-Write a c++ programme to read and print student details using structure.
#include<iostream>
Using namespace std;
Struct student
{
int rolno;
char name[20];
float mark;
};
int main()
{
struct student s;
cout<<”enter the rolno”;
cin>>s.rolno;
cout<<”enter the name”;
cin>>s.name;
cout<<”enter the mark”;
cin>>s.mark
cout<<” rollno=”<<s.rolno;
cout<<”Name =”<<s.name;
cout<<”mark=”<<s.mark;
return 0;
}
NESTED STRUCTURE
It means structure with in a structure and inner most member in a nested structure
can be accessed by chaining all the conserved structure Variable [ outer most to inner
most] with member using dot operator (.)
Struct pay
{
Int basic;
Int da;
};
Struct employ
{
Int id;
Char name[10];
Struct pay p;
}
Void main()
{
Struct employ e;
e.id=101;
e.name=“abc”;
e.p.basic=10000;
e.p.da=101.5;
}
student s = {4325, "Vishal", {10, 11, 1997}, 575};
cout<<s.adm_no<<s.name;
cout<<s.dt_adm.day<<"/"<<s.dt_adm.month<<"/"<<s.dt_adm.year;
outer_structure_varaiable.inner_structure_variable.element
ARRAY VS STRUCTURE
POINTERS
A pointer is a derived data type.
It contain memory address of another variable.
It can be used to access & manipulate data stored in the memory.
BENEFITS OF POINTERS
• Pointers are more efficient in handling arrays.
• Pointers can be used to return multiple values from a function.
• Pointers support dynamic memory management.
• Pointers provide an efficient tool for manipulating dynamic data
structures such as data structures linked list , queue.
• Pointers reduced length and complexity of a programme.
• Pointers increases the execution speed.
int num=25;
variable is associated with two values:
L-value and R-value,
where L-value is the address of the variable and Rvalue is its
content.
Figure 1.5 shows that L-value of num is 1001 and R-value is 25
DECLARING POINTER VARIABLE
The declaration of a pointer variable take the following forms,
Data type * pointer_variable_name;
Eg:
int *p;
int *ptr1;
float *ptr2;
struct student *ptr3;
The address of a variable can be assigned to pointer variable using
address operator (&)
Eg:
int *p;
int a;
p=&a
ptr1 = #
cout<< #
cout<< ptr1;
cout<< num;
cout<< *ptr1;
cout<< &ptr1;
cout<< *num;
cout<< # // 1001 (address of num) will be the output
cout<< ptr1; // 1001 (content of ptr1) will be the output
cout<< num; // 25 (content of num) will be the output
cout<< *ptr1;/* 25 (value in the location pointed to by
ptr1) will be the output */
cout<< &ptr1;// 1500 (address of ptr1) will be the output
cout<< *num; // Error!! num is not a pointer
The process of assigning address of a variable is known as initialization.
initialization:- int a;
int *p=&a;
àA pointer can store address of similar variable only.
Eg:- int *p;
float f;
p=&f /error
• It is possible to make a pointer to another pointer. Thus creating
the chain of pointers.
Int x,*p1,**p2;
X=100;
P1=&x;
P2=&p1
COUT<<**p2;
• A pointer can be initialized with null or zero.
• Multiplication is not possible on pointer variable.
METHODS OF MEMORY ALLOCATION
The memory allocation that takes place before the execution of the
program is known as static memory allocation.
memory is allocated during the execution of the program is called
dynamic memory allocation
NEW : ALLOCATE
DELETE : DE-ALLOCATE
DYNAMIC OPERATORS - NEW AND DELETE
The operator new is a keyword in C++ and it triggers the allocation of
memory during run-time (execution).
following syntax is used for dynamic memory allocation:
pointer_variable = new data_type;
short * si_ptr;
float * fl_ptr;
struct complex * cx_ptr;
si_ptr = new short;
fl_ptr = new float;
cx_ptr = new complex;
allocated memory locations can also be initialised using the following
syntax:
pointer_variable = new data_type(value);
The following examples show initialisation along with dynamic memory
allocation:
si_ptr = new short(0);
fl_ptr = new float(3.14);
delete operator is used with the following
syntax:
delete pointer_variable;
The following are valid examples:
delete si_ptr;
delete fl_ptr, cx_ptr;
MEMORY LEAK
Orphaned Memory Block
If the memory allocated using new operator is not freed using delete,
that memory is said to be an orphaned memory block
- a block of memory that is left unused, but not released
for further allocation. This memory block is allocated on each execution
of the program and the size of the orphaned block is increased. Thus a
part of the memory seems to disappear on every run of the program, and
eventually the amount of memory consumed has an unfavorable effect.
This situation is known as memory leak.
The following are the reasons for memory leak:
• Forgetting to delete the memory that has been allocated dynamically
(using new).
• Failing to execute the delete statement due to poor logic of the
program code.
• Assigning the address returned by new operator to a pointer that was
already pointing to an allocated object