Breaking News

Pointers In C++

 
Pointers are variables that store memory address. This address is the address of another variable. Hence a pointer points to another variable.
 
Declaring a Pointer:
 
Syntax
 
data type * name of pointer ;
 
When * is used, it denotes a pointer. The data type denotes the data type to which the pointer points.
 
Let’s see an example:
 
{
int marks = 6; // marks is a variable value of integer type initialized to 6
int * pmarks; // pmarks is a pointer that points to an integer data type.
pmarks = &marks; // pmarks is assigned the address of marks which is a integer.
cout<< *pmarks; // Displays the value of 6. Explanation given below.
cout<< pmarks; // Displays a memory address.
}
 
We think the above program (you have to write the usual void main and # include statements) is self explanatory. The only doubt you may have is what does cout<< *pmarks; do?
 
In this context * is known as a dereferencing or indirect value operator. This operator will give the value stored at a particular location. It is the complement of the & operator that you saw in the previous section. Hence, *pmarks means value at pmarks (which holds an address).
 
To summarize we can say that * means ‘value at the address’.
 
The two operators * and & can be considered as pointer operators.
 
The difference between pointers and a general variable is that address is primary for pointers. The value stored at the address is secondary.
 
For other variables, the value is primary while the address where the value is stored is secondary. The example below illustrates primary and secondary:
 
{
int x = 5; // x is an integer
int * p; // p is a pointer
p = &x; // p points to x
cout<<p; // This displays the address. Hence address is primary for pointer
cout<<*p; // Only using * operator can we display the value. Hence value is secondary. cout<<x;           // For the integer, value is primary.
This displays 5
 
cout<<&x; // Need to use & to display address. Hence address secondary
}
 
Be careful with Pointers
This section deals with some of the mistakes that can be committed using pointers
 
Note: Make sure that your pointer variable points to the correct type of data. If you declare a pointer of type int then make sure that the address it holds points to an integer.
 
float y = 6.6; // y is a float quantity
int * p; // p points to an integer type
p = &y; // WRONG – p points to an integer but y is a float. Hence &y is address of a float.
 
Thus the above part of a program is wrong.
 
Suppose you want to declare two pointers: p1 and p2 that point to an integer data type. You might right the declaration as:
 
int * p1, p2;
 
Seems right, doesn’t it? Well the declaration is WRONG. What this does is, declares p1 as a pointer pointing to integer type.
 
The compiler will consider p2 as an integer and not a pointer. Since * precedes p1, the compiler knows that p1 is a pointer. Hence be careful while declaring pointers.
 
Consider an extract from a program given below:
 
long * marks; // marks is a pointer that points to data type long. Statement correct.
* marks = 223000; // WRONG
 
Note the point that we have not yet stored any memory address in marks.
 
When a pointer is created the compiler will allot memory to hold an address. It will not allocate memory to hold data to which address points. Perhaps it’s a bit confusing. Just read on…
 
The problem with our program is that we are supposed to store an address in marks. After storing an address in marks you can then store some value at that address.
 
What we have done is : we haven’t assigned any address to marks. Hence the value 223000 cannot be placed anywhere.
 
Pointers are not the same as integers. Pointers hold a memory address and hence they cannot be multiplied or divided. Regarding addition We shall discuss that later. Consider the following:
 
int * p; // Correct. Declaring a pointer that points to an integer.
p = 0xB8000000 ; // WRONG
 
We said that a pointer stores a memory address. So you may think that the second line is correct. We have assigned a hexadecimal number to p (which is a pointer).
 
The problem is: the compiler doesn’t know that 0xB8000000 is a memory address. You may get an error message saying data type mismatch. Instead you can write as:
 
p = (int * ) 0xB8000000 ; // This is correct.
 
Performing addition on Pointers
 
We said that you can’t perform multiplication and division on pointers. But you can do addition and subtraction on pointers.
 
Suppose that p1 is a pointer that points to an integer and the address is 100 (yes, addresses should be hexadecimal numbers but assume 100 as a memory address for simplicity).
 
Also assume that an integer occupies 2 bytes. Now we say:
 
p1++ ;
 
What would the above expression do? The value of p1 (i.e. the address of p1) gets changed. The address becomes 102 not 101 because an integer occupies 2 bytes. If an integer occupied 4 bytes then the result would be address 104. Similarly if you do:
 
p1–;
 
the address of p1 will be 98.
 
Hence, when a pointer is incremented it points to the memory location of the next element of its data type.
 
Similarly you could say the following:
 
p1=p1+10;
 
This makes p1 point to the tenth element of p1’s data type beyond the current position. If p1 points to a character type then p1 + 10 would move the pointer 10 bytes. Suppose p1 were a integer then p1 would move 20 bytes (if int occupies 2 bytes).
 
Pointers and Arrays
 
Arrays can be accessed using pointers instead of their index numbers. Consider the following program:
 
 
out put of the program :
 
 
When you say *(p + 1), the pointer adds one to itself (this means the pointer increments itself by the number of bytes of its data type). Hence it will go to the next integer address which is array[1]. The output will thus be 5.
 
Consider a modification of the above program. The code is the same till array[2] = 6; After this line type in the following code:
 
p = p+1; // When pointer increments by one, it goes to the next integer address
 
cout<<endl<<*p; // p now is having address of array[1]. Hence output is 5
cout<<endl<<*(p+1); // p is further increased by one. This now points to array[2].
out put is 6
}
 
This pointer :
 
Every C++ class has an implicit pointer called the this pointer. The this pointer points to the instantiation of the object. The this pointer can be de referenced to members of the class like any other pointer.
 
Inside of a class declaration the use of the this pointer is implicit, so the code this->destroy(); is equivalent to destroy();
 

Check Also

Insertion & Extraction In C++

Insertion is the operation of sending characters to a stream, expressed by the overloaded insertion …