Friday, 9 January 2015

Pointers in C++ with examples

Pointers in Cpp with examples:

Pointers are very easy in C++. If we understand correctly we can play with the pointers, otherwise they will play with us. As a C++ developer, we have to know about the pointers.
When we define a variable, the memory allocated for that and that memory location have its address. That address can be accessed using & operator, which defines the address in the memory location.
 Let us look into the following program, which will print the address of the variables defined:
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
   int  iVal1;
   char iVal2[10];
   cout << "Address of iVal1 is: ";
   cout << &iVal1 << endl;
   cout << "Address of iVal2 is: ";
   cout << &iVal2 << endl;
   return 0;

The output of the above program is
Address of iVal1 is: 0056EF20

Address of iVal2 is: 0056EF30

What Are Pointers in c++?

A pointer is a variable whose value is the address of another variable.

The declaration of the pointer variable is:

datatype *Nameofvariable

Here, datatype is the pointer's base type; it must be a valid C++ type and Nameofvariable is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication.

However, in this statement the asterisk is being used to designate a variable as a pointer. Let us see some of the valid pointer declaration:

int    *ipInt;    // pointer to an integer
double *dpDbl;    // pointer to a double
float  *fpFlt;    // pointer to a float
char   *chChar     // pointer to character

The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.

How to use pointers in C++?

We should know the some important operations mentioned below:
Define a pointer variable
Assign the address of a variable to a pointer.
Access the value at the address available in the pointer variable.

This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.

Let us see the below example.
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
   int  iVal = 20;   // actual variable declaration.
   int  *ipVal;        // pointer variable
   ipVal = &iVal;       // store address of iVal in pointer variable
   cout << "Value of iVal variable: ";
   cout << iVal << endl;
   // print the address stored in ipVal pointer variable
   cout << "Address stored in ipVal variable: ";
   cout << ipVal << endl;
   // access the value at the address available in pointer
   cout << "Value of *ipVal variable: ";
   cout << *ipVal << endl;
   return 0;

When the above code is compiled and executed, the output is as follows:
Value of iVal variable: 20

Address stored in ipVal variable: 0044EC2C

Value of *ipVal variable: 20

No comments: