Pointer
In C++, a pointer is a variable that stores the memory address of another variable. Pointers provide a powerful mechanism for working with memory and data structures. They allow direct manipulation of memory addresses and enable efficient memory management.
Declaring Pointers
To declare a pointer, you use the data type followed by an asterisk (*) and the pointer name. For example:
int *ptr; // Declares a pointer to an integer double *dblPtr; // Declares a pointer to a double
Initializing Pointers
Pointers should be initialized before use. You can initialize a pointer by assigning the address of a variable to it. The address-of operator (&) is used to obtain the memory address of a variable.
int num = 10; int *ptr = # // Initializes ptr with the address of num
Dereferencing Pointers
Dereferencing a pointer means accessing the value stored at the memory address it points to. The dereference operator (*) is used for this purpose.
int num = 10; int *ptr = # cout << *ptr; // Prints the value stored at the address pointed by ptr
Example 1:
#include <iostream> using namespace std; int main() { // Declaration of a pointer to an integer int *ptr; // Declaration and initialization of an integer variable int val = 5; // Assigning the address of 'val' to the pointer 'ptr' ptr = &val; // Printing the address stored in the pointer cout << ptr << endl; // Printing the value stored at the address pointed by 'ptr' cout << *ptr << endl; // Return 0 to indicate successful execution return 0; }
This code declares a pointer, initializes an integer variable, assigns the address of that variable to the pointer, and then prints both the pointer’s value (memory address) and the value it points to.
Here are the key points:
- Pointer Declaration:
int *ptr;: Declares a pointer to an integer. This pointer is currently uninitialized. - Variable Declaration and Initialization:
int val = 5;: Declares an integer variable named val and initializes it with the value 5. - Assigning Address to Pointer:
ptr = &val;: Assigns the memory address of the variable val to the pointer ptr. Now ptr “points” to the memory location of val. - Printing Pointer’s Value (Memory Address):
cout << ptr << endl;: Prints the memory address stored in the pointer ptr. The address is typically represented in hexadecimal format. - Printing Value at the Address Pointed by Pointer:
cout << *ptr << endl;: Uses the dereference operator (*) to access the value stored at the memory address pointed by ptr. Prints the value stored in the variable val.
Output:
If you run this program, the output will be something like:
0x7ffee3c86a4c // The memory address (this will vary on each run) 5 // The value stored at the memory address
Example 2:
#include <iostream> using namespace std; int main() { int *p; int v = 9; p = &v; cout << *p << endl; return 0; }
This code prints the address of the value pointed to by the pointer p using the expressions &*p.
cout << &*p << endl;: Uses the dereference operator (*) to access the value stored at the memory address pointed by p. The & operator then takes the address of this value. Essentially, &*p is equivalent to just p, so this line prints the memory address stored in the pointer p.