Pointer is an important part of the programming as it deals with the memory address of the variables. There are various tasks that can be achieved easily with the use of pointers in C++ such as dynamic memory allocation that needs pointers for its operation. It also minimizes the execution time and saves memory space. Arrays, function and structure also use pointers to manipulate the variables and parameters.
Let us understand the memory address first.
Memory Address:
As we already know at this point that every variable declared in a program is stored in computer storage possessing some memory address. And these addresses can be accessed by using the ampersand (&) operator. If you have the variable name var then &var
is used to access the address of that variable in a program.
Let us see ampersand (&) in action.
C++ program to print the address of variables using pointers.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <iostream> using namespace std; int main() { //variables declaration and initialization int var1 = 8; int var2 = 50; // printing the address with & cout << "Address of var1: "<< &var1 << endl; cout << "Address of var2: " << &var2 << endl; } |
Output:
1 2 | Address of var1: 0x7ffec209b710 Address of var2: 0x7ffec209b714 |
The address on displayed on your computer may be different. It address is in hexadecimal representation.
C++ Pointer
In programming, pointers refer to the symbolic representation of an address. Pointers are the variables that store the address of another variable (the variable the pointer is pointing to) rather than the value itself.
Syntax of Pointer:
Declaration of pointer in C++: Pointer variables are declared in the following way using asterisk * symbol ( asterisk * = same asterisk used for multiplication).
1 2 | //pointer declaration data_type *pointer_name; |
Example: You can declare the pointer with different C++ data-types too, some are:
1 2 3 | int *intp; //pointer to an integer float *fltp; //pointer to a float char *chp //pointer to a character |
Initialization of pointers
After declaring the pointer, we need to initialize the pointers. To get the actual address we use the ampersand (&) operator and it is used before the variable name that pointer needed to be the point.
1 | pointer = &variable_name; |
Example:
1 2 3 4 5 | int *ptr; int x =10; x = 10; ptr= &x; |
- Reference operator (
&
): It returns the variable’s address. - Dereference operator (
*
): It gets the value that has been stored in a memory address.
How to use a Pointer?
- First, we define a pointer.
- Then, assigning an address of a variable to a pointer using a Reference operator (
&
). - Lastly, with the use of Dereference operator (
*
), we access the value present at the address pointed by the pointer variable.
Pointer Example
Let us see a C++ program to print the address and the value with Pointers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; int main() { //variables declaration and initialization int num = 10; int *ptr; //pointer variable //ptr stores the address of num variable ptr = # // printing the address with & cout << "Address of num: "<< ptr << endl; cout << "Value of *ptr variable: " << *ptr << endl; } |
Output:
1 2 | Address of num: 0x7ffedcadffec Value of *ptr variable: 10 |