LAB 14 Pointers: 14.1 Objectives
LAB 14 Pointers: 14.1 Objectives
LAB 14 Pointers: 14.1 Objectives
POINTERS
14.1 OBJECTIVES
For a C++ program, the memory of a computer is like a succession of memory cells, each one byte in
size, and each with a unique address. These single-byte memory cells are ordered in a way that allows
data representations larger than one byte to occupy memory cells that have consecutive addresses.
This way, each cell can be easily located in the memory by means of its unique address. For example, the
memory cell with the address 930 always follows immediately after the cell with address 929 and
precedes the one with 931, and is exactly one hundred cells after 830 and exactly one hundred cells
before 1030.
When a variable is declared, generally an operating system allocates a specific memory location to that
variable. Of course it is needed to store the value, which the variable is going to hold.
To obtain an address of a variable, we use an ampersand sign (&), known as address-of operator with the
name of variable. For example:
Note: Memory addresses are usually represented in hexadecimal (base 16) notation. That is why you will
see output like above.
14.2.2 POINTERS
We have seen variable types that store characters, integers, floating-point numbers, and so on.
Addresses are stored similarly. A variable that holds an address value is called a pointer variable, or
simply a pointer. The declaration of pointers follows this syntax:
type * name;
where type is the data type pointed to by the pointer. This type is not the type of the pointer itself, but
the type of the data the pointer points to. For example:
1 int * number;
2 char * character;
3 double * decimals;
These are three declarations of pointers. Each one is intended to point to a different data type, but, in
fact, all of them are pointers and all of them are likely going to occupy the same amount of space in
memory (the size in memory of a pointer depends on the platform where the program runs).
Pointers do not only give us the privilege to track a variable exactly in memory. Also we can have an
indirect access to the values/contents of the variables to which pointers are pointing. To make it more
clear see the following example:
int *ptr;
int k = 5;
ptr = &k;
cout<< “Address of k is: ” << ptr << endl;
cout<< “Value of k is: ” << *ptr << endl;
In the last line, the * with pointer variable ptr is used as a dereference operator or also called as
indirection operator. It means “the value of the variable pointed to by”. Thus the expression *ptr
represents “the value of the variable pointed to by ptr”.
See the following diagram to have better visualization of memory.
Activity 1:
Run the following code on your computer and observe how the pointers are playing with memory.
#include<iostream>
using namespace std;
int main()
{
int a= 5;
int b= 20;
int *ptr1, *ptr2;
ptr1= &a;
ptr2= &b;
cout<<ptr1<<"\t"<<ptr2<<endl;
cout<<*ptr1<<"\t\t"<<*ptr2<<endl;
a--; b++;
cout<<ptr1<<"\t"<<ptr2<<endl;
cout<<*ptr1<<"\t\t"<<*ptr2<<endl;
*ptr1=5;
*ptr2=*ptr1;
ptr1=ptr2;
cout<<ptr1<<"\t"<<ptr2<<endl;
cout<<*ptr1<<"\t\t"<<*ptr2<<endl;
return 0;
}
14.2.4 ARRAYS AND POINTERS
There is a close association between pointers and arrays. Consider this declaration of an array:
int a[10];
As you know, a[0], a[1], a[2] and a[3] denotes 1st , 2nd , 3rd and 4th element of array “a” respectively. This
is called array notation to access the elements. Apart from this there is another way to access the
elements, which is called pointer notation. For example in current situation *(a+0), *(a+1), *(a+2) and
*(a+3) works the same to perform the above job for array “a”.
Let’s make it more logical by saying “the array name without brackets denotes a pointer to the starting
element”. You can capture that pointer (address) in another pointer variable like this:
You can also use the array name as a pointer in expressions. The statement
Similarly (a+1) is a pointer which is holding the address of 2nd element, (a+2) of 3rd element and so on.
The elements can be accessed by using dereference operator with any address as *(a+1). This is
providing us the 2nd element itself.
Note that, “a” is an array of integers so incrementing a pointer means jump of 4 bytes in memory. As a
result now it is pointing to the next element of the same array.
As the function is taking a pointer variable as an argument which means it must need to get an address
at the time of call. Afterwards, it should dereference the argument to manipulate the values to which it
is pointing.
Activity 2
Make a function which will be used to calculate the power of a number. Perform it through both pass by
reference and pass by pointer approaches. Print the addresses before and during the function call. Write
down your observations.