Pointers in C++

category: 

Computer memory comprises of bytes (8bits). Each and every byte of memory has an address associated with it or put it this way that each byte has a number that is called the address of the byte. We can say that a pointer is nothing more than an address. Pointers are variables that hold the addresses of variables. It is possible to access the variable “directly” through the pointer. Remember it is fruitless unless it is pointing to something. Another important feature of a pointer is that it is exactly the same size on any given machine. In programming language Pascal, pointers are used exclusively in dynamic data structures, E.g. to build linked data structures. The use of pointers in C and C++ language is very extensive and is considered the most deceptive element. In C++, pointers are not only used in dynamic data structures but also processing of arrays and strings, passing parameters into functions, struct and object oriented programming techniques.

Program using pointers should be read thoroughly again and again, because the misuse of pointers is the major cause of bugs and errors in programs. This is true because it happens most of the time not only with the beginners but also with the experienced programmers.

When the execution of a program is started, all declared variables are stored in memory. As mentioned previously, each byte in a memory has its own address. Therefore, each variable is given a unique address or location. Basically or we can say typically, a variable with its associated memory address contains a value which we know it from the definition of a variable attributes.

The basic concept of pointer is that it is declared as pointer type, it means, it is pointing to a variable of that type, if it contains the address of the machine memory, otherwise, pointing to nothing. The declaration of a pointer to a specific type is easy.

For example

int *ptr;

This declaration says that ptr is a pointer variable and it points to a location that holds a value of type int.

Pointers Declaration

int intval = 10;

Then int ptr can be set as

int *ptr;

Pointing to intval by writing the statement

ptr = &intval;

Pointers Definition

A pointer is special type of variable that holds the address of memory rather than a data value. Read this carefully, we have assigned the address of intval not the contents of initial; ptr can have an address by definition not the integer value. Suppose the intval is located on address 100, the contents of ptr variable become the address of intval not the contents of intval that is ‘10’ as shown in figure.

Figure: Pointers to Integer Assignment

Pointers-in-c++

The address of operator (&) is used in the assignment, meaning the address of the variable is to be stored inside a variable to be of type pointer to int. We can retrieve the value ‘10’ through pointer variable ptr indirectly.

For example,

cout<<””The value of intval = “<<*ptr<

So writing

*ptr

Says to fetch where ptr points to. We could also print the assumed address 100.

For example,

cout<<”The address of intval = “<

The assignment of constant value is legitimate. If we want to change the value of intval from 10 to 50, we write;

*ptr = 50;

Figure: Indirect change of value

indirecto-change-of-value

This means, store the value 50 into a location where ptr points to. This statement will override the previous value, as shown in figure. The next small segment of a program illustrates the assignment of one variable to another variable through a pointer variable.

int i, j, *ptr;

i = 30;

ptr = &i;

j = *ptr;

Assuming the memory addresses of ‘I’ and ‘j’ are 300 and 400 respectively. The storage of the three statements is shown in the figure.

Figure: Indirect access to J

pointers-indirect-acces-j

As mentioned previously, in an expression, * means “indirect” that is use the storage pointed to. We can obtain the same result in the following fashion.

j = i;

When this statement is executed, the contents of I, that is 30 will be placed in memory allocated to j. Let us make it simple.

j = 30;

There are two values associated with j. One is the integer value 30 and the other is the address of the memory location. The left side of the assignment operator (=), is the address where the result of the right side ends up. The left side is called the lvalue and the right side is called the rvalue. In the above statement, it is illegal to say 30 = j. In this case, the rvalue cannot be used on the left side of the assignment operator.

The address-of operator can only be applied to lvalue, means that have an address in the memory. A pointer variable is itself an lvalue. It has a location in the memory and can be assigned to. The indirect value of a pointer is also an lvalue.

Consider the following small segment of a c++ program.

int i = 1, *ptr;

++i;

We are familiar with this type of assignment, but now its time how to increment the value of I, using indirect accessing.

For example,

int i = 1, *ptr;

ptr = &i;

*ptr = *ptr + 1;

The assignment will cause ‘I’ to be incremented by 1, as long as, ptr points to ‘I’.

The operators such as;

* multiplication

- subtraction

/ division

are all allowed and the compound assignment operator too. In compound assignment operator, care must be taken because it pays to be careful with spaces.

The increment and decrement of a pointer ptr itself is legal and the operations can be done in the following manner.

int *ptr;

ptr = ptr + 1;

ptr = ptr – 1;

The prefix and postfix notation can also be performed on the above operations. The two operations increment and decrement by units equal to the number of locations occupied by the object pointed by ptr. The modification of the value of address stored in a pointer is as simple as the value of a normal variable.

The use of relational operators is also legal.

For example,

ptr > ptr1

This a valid expression if both are pointers. This says that ptr is greater than ptr1 if a ptr point to a memory location is higher than the memory location where ptr1 points to. Let’s try to write a complete c++ program ptrchar.cpp dealing with a pointer to a character. It is self-explanatory from the output of the program.

Pointer to Character program

  1. //program name: ptrchar.cpp
  2. #include
  3. #include
  4. int main(void)
  5. {
  6.             char ch, *chptr;
  7.             clrscr();
  8.             ch = ‘a’;
  9.             cout<<”character is :”<
  10.             ch = ‘b’;
  11.             chptr = &ch;
  12.             cout<<”character is : “<<*chptr<
  13.             *chptr = ‘c’;
  14.             cout<<”value changed by indirect access\n”;
  15.             cout<
  16.             return 0;
  17. }

Run output

character is : a

character is : b

value changed by indirect access

character is : c