Computer - Science - Notes - CH04 - Pointers Notes
Computer - Science - Notes - CH04 - Pointers Notes
in
POINTERS
- Pointer is a variable that holds a memory address of another variable of same type.
- It supports dynamic allocation routines.
- It can improve the efficiency of certain routines.
C++ Memory Map :
- Program Code : It holds the compiled code of the program.
- Global Variables : They remain in the memory as long as program continues.
- Stack : It is used for holding return addresses at function calls, arguments passed to
the functions, local variables for functions. It also stores the current state of the CPU.
- Heap : It is a region of free memory from which chunks of memory are allocated via
DMA functions.
Static Memory Allocation : The amount of memory to be allocated is known in advance and
it allocated during compilation, it is referred to as Static Memory Allocation.
e.g. int a; // This will allocate 2 bytes for a during compilation.
Datatype *variable_name;
void main
{
int *m;
int marks[10] ={ 50,60,70,80,90,80,80,85,75,95};
m = marks; // address of first location of array or we can write it as m=&marks[0]
forint i=0;i<10;i++
cout<< *m++;
// or
m = marks; // address of first location of array or we can write it as m=&marks[0]
forint i=0;i<10;i++
cout<< *(m+i;
}
Index 0 1 2 3 4
address 1000 1002 1004 1006 1008
int a = 12, b = 23, c = 34, d = 45, e = 56;
Variable a b c d e
Value 12 23 34 45 56
address 1050 1065 2001 2450 2725
ip[0] = &a; ip[1] = &b; ip[2] = &c; ip[3] = &d; ip[4] = &e;
51
An array of char pointers is very useful for storing strings in memory. Char
*subject[] = { “Chemistry”, “Phycics”, “Maths”, “CS”, “English” };
In the above given declaration subject[] is an array of char pointers whose element pointers contain
base addresses of respective names. That is, the element pointer subject[0] stores the base address of
string “Chemistry”, the element pointer subject[1] stores the above address of string “Physics” and so
forth.
An array of pointers makes more efficient use of available memory by consuming lesser number
of bytes to store the string.
An array of pointers makes the manipulation of the strings much easier. One can easily exchange
the positions of strings in the array using pointers without actually touching their
memory locations.
52
#include<iostream.h>
void main
{
void swap int &, int & ;
int a = 5, b = 6;
cout << “ n Value of a :” << a << “ and b :” << b;
swap a, b ;
cout << “ n After swapping value of a :” << a << “and b :” << b;
}
void swap int &m, int &n
{
int temp; temp = m;
m = n;
n = temp;
}
output :
Value of a : 5 and b : 6
After swapping value of a : 6 and b : 5
#include<iostream.h>
void main
{
void swap int *m, int *n ;
int a = 5, b = 6;
cout << “ n Value of a :” << a << “ and b :” << b;
swap &a, &b ;
cout << “ n After swapping value of a :” << a << “and b :” << b;
}
void swap int *m, int *n
{
int temp;
temp = *m;
*m = *n;
*n = temp;
}
Input :
Value of a : 5 and b : 6
After swapping value of a : 6 and b : 5
53
Dynamic structures :
The new operator can be used to create dynamic structures also i.e. the structures for which the
memory is dynamically allocated.
struct-pointer = new struct-type;
student *stu;
stu = new Student;
A dynamic structure can be released using the deallocation operator delete as shown below :
delete stu;
54
You can even make a pointer point to a data member of an object. Two points should be
considered :
1. A Pointer can point to only public members of a class.
2. The data type of the pointer must be the same as that of the data member it points to.
this Pointer :
In class, the member functions are created and placed in the memory space only once. That is
only one copy of functions is used by all objects of the class.
Therefore if only one instance of a member function exists, how does it come to know which object’s
data member is to be manipulated?
55
For the above figure, if Member Function2 is capable of changing the value of Data Member3
and we want to change the value of Data Member3 of Object3. How would the Member Function2
come to know which Object’s Data Member3 is to be changed?
To overcome this problem this pointer is used.
When a member function is called, it is automatically passed an implicit argument that is a pointer to
the object that invoked the function. This pointer is called This.
That is if ojbect3 is invoking member function2, then an implicit argument is passed to member
function2 that points to object3 i.e. this pointer now points to object3.
The friend functions are not members of a class and, therefore, are not passed a this pointer. The static
member functions do not have a this pointer.
Solved Questions
Q. 1 How is *p different from **p ?
Ans : *p means, it is a pointer pointing to a memory location storing a value in it. But **p means, it
is a pointer pointing to another pointer which in turn points to a memory
location storing a value in it.
Q. 2 How is &p different from *p ?
Ans : &p gives us the address of variable p and *p. dereferences p and gives us the value
stored in memory location pointed to by p.
Q. 3 Find the error in following code segment :
Float **p1, p2;
P2 = &p1;
Ans : In code segment, p1 is pointer to pointer, it means it can store the address of another pointer
variable, whereas p2 is a simple pointer that can store the address of a normal variable. So here
the statement p2 = &p1 has error.
Q. 4 What will be the output of the following code segment ?
char C1 = ‘A’;
char C2 = ‘D’;
char *i, *j;
i = &C1;
j = &C2;
*i = j;
cout << C1;
Ans : It will print A.
Q. 5 How does C++ organize memory when a program is run ?
Ans : Once a program is compiled, C++ creates four logically distinct regions of memory :
i area to hold the compiled program code
ii area to hold global variables
iii the stack area to hold the return addresses of function calls, arguments passed to the
functions, local variables for functions, and the current state of the CPU.
iv The heap area from which the memory is dynamically allocated to the program.
56
57
58