C++ Programming

Pointer Variables in C++ with Examples

Pointer Variables:

Pointer Variables- A variable that is used to store the memory address is called a pointer variable. Pointer variable is also simply called a pointer.

Usually, a pointer is used to store the memory address of another variable that contains the actual value. So pointer points to the memory address of another variable. The data type of the pointer and the variable whose address a pointer is to be stored must be the same.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Declaring Pointer Variables:

The pointer (or pointer Variables) are declared in the similar way as ordinary variables, except an asterisk (*) is place before the pointer variables name or after data type. For example, to hold the memory address of int type variable, the pointer variables are declared as:

int *pn1, *pn2;

similarly, to hold the memory address of double type variables, the pointer variables are declared as:

double *px, *py;

we can declare the ordinary variables and pointer variables in one statement also. For example:

int x, *px;


Address Operators(&):

The address operator is a unary operator. It returns the memory address of variable or object. The address operator is denoted by &. The address operator is also called the reference operator.

Usually, address operator (&) is used to get a memory address of a variable and assign to pointer variable through assignment statement. For example:

int x,*px;

x=333;

px=&x;

the value 333 is assigned to variable ‘x. suppose the memory address 20000 is allocated to variable ‘x’ and memory address 30000 is allocated to pointer variable ‘px’. After executing the statement “px=&x; “ , the contents of ‘px’ will be 20000. The representation of ‘x’ and ‘px’ is shown in the following figure

Pointer variables

The pointer variable ‘px’ is said to be pointing to variable ‘x’.

Indirection Operator (*)

The indirection operator is used with pointer variable to access the value of the variable whose memory address is stored in pointer variable. The indirection operator is denoted by *. It is also a unary operator.

It means that the data of a variable can be accessed indirectly through pointer variable using * operator. The process to access data in the variable using * operator with pointer variable is called the de-referencing the pointer. Therefore, the indirection (*) operator is also known as de-reference operator.

For example, to display the value of ‘x’ indirectly by using pointer ‘px’ , the statement is written as under (variables ‘x’ and  ‘*px’ are declared as you can see in the above figure  and value 333 is also assigned to ‘x’):

Cout<<*px;

The above statement is equivalent to:

Cout<<x;

The de-reference operator may also be used to assign a value to a variable ‘x’. for example, a statement is given below to assign a value 760 to variable ‘x’ through the de-reference operator.

*px = 760;

The value 760 will be assigned to variable ‘x’. we can also use de-reference operator to input value to a variable from keyboard during execution of a program. For example , to input value to  variable ‘x’ through *px, the statement is written as:

Cin>>*px;

Someone may be confused that the * operator is a multiplication operator.  When * operator is used as unary operator, then it is considered as indirection operator.



Programming example

Write a program that assigns values to variables using pointer variables. It computes the sum of values and displays the result on the screen:

#include <iostream>
using namespace std;

int main()
{
    int x, y, *px, *py;
    px=&x;
    py=&y;
    *px=5;
    *py=8;
    cout<<" sum of x and y = "<<x+y;
}

Pointer variables

Initializing Pointer Variables:

A pointer variables can also be initialized at the time of its declaration. A pointer may be initialized to 0, NULL, or memory address of a variable. A pointer with a value 0 or NULL, pointer to nothing. The value of 0 and NULL are equivalent.

Following statement declares a pointer variable ‘pn’ and initialize its value to NULL:
int *pn = NULL;

Or

Int *pn= 0;

Similarly, to declare a variable ‘x’, a pointer ‘px’ and also to initialize the pointer variable ‘px’ with memory address of ‘x’, the statement are written as:

Int x;

Int *px = &x;


Programming example:

Write a program that initializes a pointer with memory address of a variable. It gets a value into variable through pointer and displays the value of the variable with reference to the name of the variable:

#include <iostream>
using namespace std;

int main()
{
   float x;
   float *px = &x;
   cout<<" enter any value : ";
   cin>>*px;
   cout<<" value of variable x = "<<x;
}

Pointer variables

The void pointer variable:

We can assign the memory address of a variable to a pointer if both variable and pointer have the same type. A ‘void’ type pointer can hold memory address of variable of any data type.

For example, to declare a ‘void’ type pointer, the statement is written as;

Void *px;


Programming example:

Write a program that declares and initializes variables x of ‘int’ type y of ‘float’ type. It displays the values and address of these variables on the screen using pointer to ‘void:

#include <iostream>
using namespace std;

int main()
{
 
    int x= 235;
    float y= 23.35;
    void *pv;
    pv= &x;
    cout<<" value of x = "<<x<<endl;
    cout<<" address of x = "<<pv<<endl;
    pv= &y;
    cout<<" value of y = "<<y<<endl;
    cout<<" address of y = "<<pv<<endl;
}

Pointer variables

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button