LAB 14 Pointers: 14.1 Objectives

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

LAB 14

POINTERS
14.1 OBJECTIVES

- Introductions to computer memory and pointers


- Declaration of pointers
- Accessing the value pointed to by a pointer
- Pointer-Array duality
- Passing pointers as an argument to function

14.2 PRE-LAB READING

14.2.1 COMPUTER MEMORY

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:

int var1 = 20;


int var2 = 10;
cout<< ”address of var1\t” << &var1 <<endl;
cout<< ”address of var2\t” << &var2 <<endl;

This code will produce an output similar to this:

address of var1 0x9ffe4c


address of var2 0x8f4f3c

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).

14.2.3 DEREFERENCE OPERATOR

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:

int* p = a; // Now p points to a[0]

You can also use the array name as a pointer in expressions. The statement

cout << *a; //dereferencing of pointer a

has the same effect as the statement


cout << a[0];

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.

14.2.5 PASSING A POINTER TO FUNCTION


We know that, if a function is intended to modify variables in the calling program, these variables cannot
be passed by value, since the function obtains only a copy of the variable’s value. However, either a
reference argument or a pointer can be used in this situation.
For example you want to write a function which takes an integer as an argument and squares of it. In
case you are doing it by passing pointers as an argument, prototype will look like this:
void square(int*); //pointer as an argument to function

Definition of a function could be as:


void square(int* ptr)
{
*ptr *= *ptr; // exactly equivalent to *ptr = (*ptr) * (*ptr)
}
Now think about that time when it is being called. Upon calling this function one must has to provide the
address of a number as an argument instead of number itself.
int number= 5;
square(&number);

square(number); //WRONG CALL

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.

You might also like