In this tutorials we will study and understand the concept of Pointers in C++.
Pointers are powerful features of C++ that enables you to manipulate the data in the computer’s memory directly.
To understand pointers, you should first know how data is stored on the computer.
Every variable in a C++ program is stored in the computer memory and this memory has a unique memory address.
To know where the data is stored, C++ has an & operator. The & (reference) operator gives you the address occupied by a variable.
If var is a variable then, &var gives the address of that variable.
int var1 = 3; //print address of var1 cout << &var1 << endl;
Output:
0x7fff5fbff8ac
The 0x in the beginning represents the address is in hexadecimal form.
Pointers in c++ are variables that points to a specific address in the memory occupied by another variable. A pointer is a variable whose value is the address of another variable. Like any variable or constant, you must declare a pointer before you can work with it. The general form of a pointer variable declaration is:
type *var-name;
Here the type is the type of variable the pointer will point to. The * symbol denotes that this variable is a pointer variable.
Following are the valid pointer declaration:
int *ip; // pointer to an integer double *dp; // pointer to a double float *fp; // pointer to a float char *ch // 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 which is of 2-bytes. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.
Reference operator (&) and Deference operator (*)
Reference operator (&) as discussed above gives the address of a variable.
To get the value stored in the memory address, we use the dereference operator (*).
For example: If a number variable is stored in the memory address 0x123, and it contains a value 5.
The reference (&) operator gives the value 0x123, while the dereference (*) operator gives the value 5.
Example Program of Pointers in C++
#include <iostream> using namespace std; int main () { int var = 20; // Variable declaration. int *ip; // pointer variable ip = &var; // store address of var in pointer // print value inside the variable cout << "Value of var variable: "<<var<<endl; // print address of variable var cout << "Address of var variable: "<<&var<<endl<<endl; // print value inside the pointer ip cout << "Value inside pointer ip: "<<ip<<endl; // print value of variable using pointer ip cout << "Variable value using Pointer: "<<*ip; return 0; }
Pointers can be used in different ways to perform different types of task. Some uses, variations and topics of pointer are as follows:
- Pointer to Arrays
- Pointer to Pointer
- Pointer to Functions
- Pointer with structures
- Dynamic Memory Allocation
- Null Pointer
View Comments (2)
i like it.it will help me alot
Thank you very much buddy. Please do share this article with your friends and contacts :-)