A pointer in C++ allows us to directly access data stored at a particular memory location. Pointers are one of the most powerful feature of C++ programming language. Once you master the use of pointers, you will use them everywhere to make the code more efficient and faster. In C++, some operations can be performed more efficiently using pointers like dynamic data structures like linked list and trees,dynamic memory allocation, pass arguments to a function as Call by Reference, access array elements etc.
What is a Pointer
A pointer in C++ is a variable which is used to store the address of another variable.
A variable in C++ is the name given to a memory location, where a program can store data. A program can manipulate the value stored using variable's identifier. When we declare a variable in C++, compiler allocates sufficient space in memory to store the value assigned to this variable. We can access the value of this variable either by variable identifier or by directly accessing the memory location using pointers.
Every memory location is given a unique address, once you know the address of a memory location(variable), you'll then be able to go to that address and manipulate the data stored in it. To get the address of any variable, we can use &(Address of) operator and to retrieve the value stored at a memory location we can use *(Value of Operator).
- If count is a variable then, &count gives the memory address of count variable.
- If ptr is a pointer variable storing the address of a memory location then *ptr gives the data stores at memory location pointed by ptr.
- We can dynamically allocate or deallocate space in memory at run time by using pointers.
- We can pass arrays to a function as call by Reference.
- The use of pointers results into faster execution of program.
- Using pointers we can return multiple values from a function.
- Pointers in C++ are used to efficiently implement dynamic Data Structures like Queues, Stacks, Linked Lists, Tress etc.
- Pointers are used to efficiently access array elements, as array elements are stored in adjacent memory locations. If we have a pointer pointing to a particular element of array, then we can get the address of next element by simply incrementing the pointer.
Reference operator (&) and Deference operator (*)
The & is a unary operator in C++ which returns the memory address of a variable. This is also known as address of operator.
The * is a unary operator which returns the value stored at a memory location which is pointed by a pointer variable. It is known as "value of" operator. It is also used for declaring pointer variable.
int A = 100; int *ptr = &A; cout << *ptr;
In the first statement, we first declare an integer variable and initialize it with value 100. In second statement, we are declaring a pointer to a variable of type int and initializing it with address of A. The third statement prints the value stored at the memory location pointed by ptr, which is 100.
Pointer Declaration
A pointer is a derived data type that is created from fundamental data types. We use (*) for defining pointer variables. Here is the syntax of declaring a pointer variable.
<data_type> *<identifier>;For Example :
int *ptr;
Above pointer declaration specifies that, ptr is a pointer variable that will store the memory address of an integer. We can also initialize a pointer as follows:
int age = 10; int *ptr = &age;
Integer variable 'age' is the name to a memory location where value 10 is stored. Let the memory address of age is "0x52436721". Then pointer variable ptr will contain address of age which is "0x52436721"
#include <iostream> using namespace std; int main () { int age = 10; int *ptr = &age; cout << "Value of count variable is " << age << endl; cout << "Address of count variable: " << ptr << endl; cout << "Value retrieved through pointer : " << *ptr; return 0; }
Output
Value of count variable is 10 Address of count variable: 0x22fe34 Value retrieved through pointer : 10
Size Of Pointer Variable
The size of a pointer variable depends on system architecture. A memory address is considered as integer value. Size of a pointer is fixed, it doesn't depend on the data type it is pointing to. We can use size of operator to get the size of a pointer.
C++ Program to print the size of pointer// C++ Program to print the size of pointer variables #include <iostream> using namespace std; int main() { int count = 10; float sum = 20.5; int *null_ptr = NULL; int *int_ptr = &count; float *float_ptr = ∑ cout << "Size of NULL Pointer : " << sizeof(null_ptr) << " Bytes\n"; cout << "Size of Integer Variable : " << sizeof(count) << " Bytes\n"; cout << "Size of Integer Pointer : " << sizeof(int_ptr) << " Bytes\n"; cout << "Size of Float Variable : " << sizeof(sum) << " Bytes\n"; cout << "Size of Float Pointer : " << sizeof(float_ptr) << " Bytes\n"; return 0; }
Output
Size of NULL Pointer : 8 Size of Integer Variable : 4 Size of Integer Pointer : 8 Size of Float Variable : 4 Size of Float Pointer : 8