


Khusboo Tayal
A pointer is a variable that stores the memory address of another variable. Pointers allow you to access and manipulate the value of a variable directly through its memory address.
To declare a pointer, use the * (asterisk) symbol before the pointer name.
data_type *pointer_name;
#include <iostream>
using namespace std;
int main() {
int number = 10;
int *ptr = &number; // Pointer storing the address of number
cout << "Value of number: " << number << endl;
cout << "Address of number: " << &number << endl;
cout << "Pointer value (address): " << ptr << endl;
cout << "Value at pointer: " << *ptr << endl;
return 0;
}Value of number: 10 Address of number: 0x61fe1c Pointer value (address): 0x61fe1c Value at pointer: 10
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30};
int *ptr = arr;
cout << "Pointer pointing to: " << *ptr << endl; // 10
ptr++; // Move pointer to next element
cout << "Pointer pointing to: " << *ptr << endl; // 20
return 0;
}
Pointer pointing to: 10 Pointer pointing to: 20
A null pointer points to nothing. It is declared using nullptr (C++11 and later).
#include <iostream>
using namespace std;
int main() {
int *ptr = nullptr;
if (ptr == nullptr) {
cout << "Pointer is null." << endl;
}
return 0;
}Pointer is null.
A void pointer can store the address of any data type but cannot be dereferenced directly.
#include <iostream>
using namespace std;
int main() {
int num = 42;
void *ptr = # // Void pointer pointing to an integer
// Dereferencing after typecasting
cout << "Value of num: " << *(int *)ptr << endl;
return 0;
}
Value of num: 42
A dangling pointer points to a memory location that has been freed.
#include <iostream>
using namespace std;
int* createPointer() {
int a = 10;
return &a; // Returning address of a local variable (Dangling)
}
int main() {
int *ptr = createPointer();
cout << "Dangling pointer value: " << *ptr << endl; // Undefined behavior
return 0;
}int value = 10; const int *ptr1 = &value; // Pointer to constant int *const ptr2 = &value; // Constant pointer
A pointer that stores the address of another pointer.
#include <iostream>
using namespace std;
int main() {
int value = 20;
int *ptr = &value;
int **ptr2 = &ptr;
cout << "Value: " << value << endl;
cout << "Pointer value: " << *ptr << endl;
cout << "Double Pointer value: " << **ptr2 << endl;
return 0;
}
Value: 20 Pointer value: 20 Double Pointer value: 20
Dynamic memory is allocated using the new keyword and freed using delete.
#include <iostream>
using namespace std;
int main() {
int *ptr = new int; // Allocate memory
*ptr = 100;
cout << "Dynamically allocated value: " << *ptr << endl;
delete ptr; // Free memory
return 0;
}
Dynamically allocated value: 100
A reference is an alias for another variable. It must be initialized when declared and cannot be changed later.
data_type &reference_name = variable_name;
#include <iostream>
using namespace std;
int main() {
int value = 50;
int &ref = value; // Reference to value
cout << "Value: " << value << endl;
cout << "Reference: " << ref << endl;
ref = 100; // Changing value using reference
cout << "Updated Value: " << value << endl;
return 0;
}
Value: 50 Reference: 50 Updated Value: 100
| Aspect | Pointers | References |
|---|---|---|
| Declaration | int *ptr; | int &ref = var; |
| Can be Null | Yes (nullptr) | No |
| Reassignment | Can point to a different variable | Cannot be changed once initialized |
| Dereferencing | *ptr | Direct use (no *) |
| Memory Required | Extra memory for pointer variable | No extra memory |
| Use Cases | Dynamic memory, data structures | Function parameters, aliases |
In this tutorial, you learned: