In C++, a pointer is a variable that stores the memory address of another variable. Pointers are powerful tools that allow for efficient manipulation of data, dynamic memory management, and the implementation of complex data structures like linked lists, trees, and graphs. Pointers can also be used to simulate call-by-reference in functions, enabling functions to modify variables directly.
A pointer variable is declared using an asterisk (*) before the variable name. The pointer must be of the same data type as the variable it will point to.
datatype *pointer_name;
int *ptr; // ptr is a pointer to an integer
char *cptr; // cptr is a pointer to a character
To assign a pointer to a variable, use the address-of operator (&), which returns the memory address of the variable.
int x = 10;
int *ptr = &x; // ptr now holds the address of x
The dereference operator (*) is used to access or modify the value stored at the memory address the pointer holds.
int x = 10;
int *ptr = &x;
int y = *ptr; // y is now 10, the value stored at the address held by ptr
Pointers can be incremented or decremented to move to the next or previous memory location. The size of the data type determines the number of bytes the pointer moves.
int arr[] = {10, 20, 30};
int *ptr = arr;
ptr++; // Now ptr points to the second element of the array (20)
Pointers can directly manipulate memory and efficiently manage dynamic data structures like arrays, linked lists, trees, etc.
Functions can return multiple values by modifying variables passed by pointers.
Pointers provide direct access to memory, enabling low-level programming and memory manipulation.
Pointers are used to allocate memory dynamically at runtime using functions like malloc() or new in C++.
int *p = new int; // dynamically allocates memory for an integer
*p = 5;
Pointers are extensively used with arrays, functions, and structures to manipulate data efficiently.
| Symbol | Name | Description |
|---|---|---|
| & | Address operator | Determines the address of a variable. |
| * | Indirection operator | Accesses the value at the address held by the pointer. |
A void pointer is a pointer that can point to any data type. It is declared as void *.
void *ptr;
int x = 10;
ptr = &x; // ptr now points to an integer
Note: A void pointer cannot be dereferenced directly. It must first be cast to another pointer type.
A null pointer is a pointer that does not point to any memory location. It is typically used to signify that the pointer is not currently pointing to any valid object.
int x = 10;
int *ptr = &x;
int **pptr = &ptr; // pptr is a pointer to the pointer ptr
An invalid pointer is a pointer that does not point to a valid memory location. This can occur if the pointer is uninitialized or points to a memory location that has been freed or is out of bounds.
Can be reassigned to point to different objects.
Can be null or uninitialized.
Use the * operator to dereference and access the value.
Must be initialized when declared.
Cannot be null or reassigned.
Use the reference operator & to create a reference.
<#include <iostream>>
int main() {
int var = 20;
int *ptr; // Declaration of a pointer variable
ptr = &var; // Pointer stores the address of var
std::cout << "Value of var: " << var << std::endl;
std::cout << "Address of var: " << &var <<
std::endl;
std::cout << "Value stored in ptr (address of var): " << ptr
<< std::endl;
std::cout << "Value pointed to by ptr: " << *ptr <<
std::endl;
return 0;
}
Value of var: 20
Address of var: 0x7ffe04e07944
Value stored in ptr (address of var): 0x7ffe04e07944
Value pointed to by ptr: 20