Pointer Variables in C++ with Examples

Pointer variables

Pointer Variables:

Pointer Variables:- A variable that is used to store the memory address is called a pointer variable. The pointer variables 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.



Declaring Pointer Variables:

The pointer (or pointer Variables) are declared in a similar way as ordinary variables, except an asterisk (*) is placed before the pointer variables name or after the 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 a variable or object. The address operator is denoted by &. The address operator is also called the reference operator.

Usually, the address operator (&) is used to get a memory address of a variable and assign it to the pointer variables 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 variables to access the value of the variable whose memory address is stored in the 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 a pointer variable using the * operator. The process to access data in the variable using the * operator with the pointer variable is called the de-referencing the pointer. Therefore, the indirection (*) operator is also known as a 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 the de-reference operator to input value to a variable from the keyboard during the 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 the * operator is used as a unary operator, then it is considered as an indirection operator.

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

Pointer variables


Initializing Pointer Variables:

A pointer variable 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.

The following statement declares a pointer variable ‘pn’ and initializes 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 the memory address of ‘x’, the statement is written as:

Int x;

Int *px = &x;

Write a program that initializes a pointer with the 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:

Pointer variables



The void pointer variable:

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

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

Void *px;

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 a pointer to ‘void:

Pointer variables


Related Article:

Constructors and Destructors in Single and Multiple inheritance in C++

 

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: