Introduction of Pointer in C++:
When variables are declared, computer reserves space in its memory for some objects. The variable name refers to that memory space that is occupied by that object.
This allows us to store the value of those variables i the space reserved. Computer refers to these spaces using their addresses. To refer to memory addresses, C++ provides the facility of pointers.
The objective of pointers is to understand how variables are stored in computer memory and how spaces are allocated to them. Sometimes, a programmer needs to refer to another memory location which can only be done by the use of pointers.
Pointer is one of the of the most powerful tool to handle memory addresses.
This article cover the concept of memory addresses in detail and explains the working of reference operator & and dereference operator * with the help of examples. It also focuses on how to declare pointer type variables and how to initialize them in a program.
After this article you will be able to understand.
Definition of pointers
Understanding Memory Address
Know the Use of Reference Operator &
Know the use of Dereference Operator *
Declare variable of Pointer types
Initialize the Pointers
Definition of Pointers.
Pointer is an important and powerful feature o C++ language, in which a variable points to another variable by holding its address.
A pointer is a variable that holds the address of another variable. In memory, each and every variable has an address assigned to it by the compiler and if a programmer wants to access that address, another variable called “pointer” is needed.
For the declaration of pointer, an asterisk* symbol is used between the type and the variable name.
Consider the following pointers declaration of the integer variable marks, floating point variable percentage and character type variable name:
int * marks;
float * percentage;
char * name;
Memory Addresses in Pointer.
When writing a program, variable need to be declared. Declaration of variable is simple and its declaration tells the computer to reserve space in memory for this variable.
The name of variable refers to that memory space. This task is automatically performed by the operating system during runtime. When variables are declared, programmers store data in these variables.
Computer refers to that space using an address. Therefore, everything a programmer declares has an address. It is analogous to the home address of some person. Using pointer variables, one can easily find out the address of a particular variables.
Reference Operator &.
As pointers are the variable which hold the addresses of other variables, therefore, while assigning addresses to them, a programmer needs a special type of operator called address-of operator that is denoted by ampersand & symbol. This provides address of a memory location.
To understand it, consider the following segment of code:
float * fpointer;
fpointer;=&x; //assign address of x to fpointer.
Consider the following program;
/*use of pointer in a program*/
float * fpointer;
cout<<“The address of x=”<<&x<<endl;
cout<<“The value of x=”<<x<<endl;
cout<<“The value of fpointer=”<<fpointer;
Output of the program.
The address of x=1000
The value of x=5.5
The value of fpointer =1000
This program gives some other output (address) on another computer depending upon the availability of the memory.
Dereference Operator *.
pointers are used to store the address of another variable. If we want to store the value of the variable through the pointer, then we need a special type of operator called dereference operator denoted b asterisk*. To use dereference operator, precede the pointer variable with asterisk* sybmol. This operator can be read as “value pointed by”.
Consider the following program to demonstrate the use of dereference operator*.
/*use of dereference operator (*) in a program */
int *pn; //defines a pointer to n
pn=&n; //pn stores the address of n
cout<<“The address of n=”<<&n<<endl;
cout<<“The value of n=”<<n<<endl;
cout<<“The value of pn=”<<pn<<enld;
cout<<“The value of *pn=”<<(*pn)<<endl;
cout<<“The value of valueN=”<<vallueN;
Output of the program
The address of n= 0x8fc5fff4
The value of n=100
The value of pn=0x8fc5fff4
The value of (*pn)=100
The value of valueN=100
In this example, the instruction at lines 10 and 14 make use of the dereference operator* and thus access the actual values of the original variable n which pointed out by the pointer pn.
In pointers, the ampersand operator & is the reference operator and can be read as”address of” and * is the dereference operator that can be read as “value pointed by”. These operator are complementary of each other and have opposite meanings. A variable referenced with & can be dereference with*.
Declaring Variable of Pointer type.
The declaration of pointer is simple and is similar to the declaration of a regular variable with a minor difference of the use of an asterisk* symbol between the data type and the variable name.
Consider the following example
Data type * nameVaraible;
Here, data type is the type the value of that variable to which this pointer will point. This data type is not the type of the pointer itself but the type of the data the pointer points to.
To understand it, consider the following example of pointers declaration.
In this example, three pointers totalMarks, Name and percentage are declared. Each one is intended to point to a different data type. All these pointers occupy space in memory.
The first pointer points to an int, the second to char and the last one to a float.
It is notable that the asterisk * symbol used between the data type and the name of he variable is the indication for the pointer declaration and is not used for multiplication.
There are three ways to place the asterisk. These are:
Placing next to the data type
The variable name
In the middle.
All these variations are shown in the above declarations of variable totalMarks, Name and percentage.
A pointer of type int can only points to a variable of type int and cannot to some other type of pointer named void pointer or generic pointer that can point to an objects of any data type.
its declaration is similar to the declaration of normal pointers with the only difference of the use of void keyword as the pointer’s type.
consider the following statement of declaration of the void pointer.
void * pointerVoid;// pointerVoid is a void pointer
As, a void pointer can point to objects of any data type, therefore, consider the following statements.
void * pointerVoid;
pointerVoid=&X; // valid
pointerVoid=&Y; // valid
In this segment of code, pointerVoid, stores the address of both integer variable X and floating point variable Y. The compiler decides at run time that the address of which type of variable should be assigned to this pointer.
Assigning values to pointers at declaration time is called pointer initialization. As we know that the values of pointers are the addresses of other variables, therefore, sometimes when we declare pointers we may want to explicitly specify to which variables they will point.
Consider the following statement of code to understand the concept of pointer initialization.
Here, pTemperature is a pointer variable to a floating point variable. As this pointer is created with the statement ‘float *pTemperature’, immediately the address of a float variable ‘Temperature’ is assigned to it. The behavior of the above code is being equivalent to the following code.
It should be considered that at the moment of declaring a pointer, the asterisk * indicates that it is a pointer variable an not the dereference operator.
Consider the following program to explain the concep of pointer initialization.
/*pointer initialization program*/
cout<<“The address of Temperatureis =”<<&Temperature<<endl;
cout<<“The value of *pTemperature is=”<<pTemperature<<endl;
Output of the program
The address of Temperature is =0x8f98fff2
The value of pTemperatureis = 0x8f98fff2
Here, the pointer pTemperature is initialized with the address of the variable Temperature.
Sometime we need to initialize a pointer to zero. Such pointers are called null pointers and they do not point to anything. Null pointers can be defined by assigning address 0 to them.
Consider the following initialization to demonstrate null pointer:
int*Nptr; //defines Null pointer
Ntpr=0; // This assigns address 0 to Nptr
The use of Null pointers is mostly done in dynamic memory allocation.