Pointers and References
Pointers and references are fundamental concepts in C++ that provide ways to manipulate memory and reference variables. Here's an overview of pointers and references, along with examples to illustrate their use:
Pointers
A pointer is a variable that stores the memory address of another variable. Pointers are powerful tools that allow for dynamic memory allocation, array manipulation, and efficient passing of variables to functions.
Declaring and Initializing Pointers
To declare a pointer, use the * operator.
Syntax:
type* pointer_name;
Example:
int x = 10;
int* ptr = &x; // ptr holds the address of x
Dereferencing Pointers
Dereferencing a pointer means accessing the value stored at the memory address the pointer points to using the * operator.
Example:
int x = 10;
int* ptr = &x;
std::cout << "Value of x: " << x << std::endl;
std::cout << "Address of x: " << ptr << std::endl;
std::cout << "Value at address stored in ptr: " << *ptr << std::endl;
Pointer Arithmetic
Pointers can be incremented or decremented to point to the next or previous memory location of the type they point to.
Example:
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr; // ptr points to the first element of the array
std::cout << "First element: " << *ptr << std::endl;
ptr++;
std::cout << "Second element: " << *ptr << std::endl;
Dynamic Memory Allocation
Using pointers, you can allocate memory dynamically during runtime with new and deallocate it with delete.
Example:
int* ptr = new int; // dynamically allocate memory for an int
*ptr = 10;
std::cout << "Value: " << *ptr << std::endl;
delete ptr; // free the allocated memory
References
A reference is an alias for another variable. Once a reference is initialized to a variable, it cannot be changed to refer to another variable. References provide a convenient way to pass variables to functions without copying them.
Declaring and Initializing References
To declare a reference, use the & operator.
Syntax:
type& reference_name = variable;
Example:
int x = 10;
int& ref = x; // ref is a reference to x
std::cout << "Value of x: " << x << std::endl;
std::cout << "Value of ref: " << ref << std::endl;
ref = 20; // changing ref changes x
std::cout << "New value of x: " << x << std::endl;
Pointers vs References
- Pointers can be reassigned to point to different variables. They can also be null.
- References must be initialized when declared and cannot be reassigned. They cannot be null.
Example Code Demonstrating Pointers and References
Here's a complete example demonstrating pointers and references in various scenarios:
#include <iostream>
void pointerExample() {
int x = 10;
int* ptr = &x;
std::cout << "Pointer Example:" << std::endl;
std::cout << "Value of x: " << x << std::endl;
std::cout << "Address of x: " << ptr << std::endl;
std::cout << "Value at address stored in ptr: " << *ptr << std::endl;
*ptr = 20;
std::cout << "New value of x: " << x << std::endl;
}
void referenceExample() {
int x = 10;
int& ref = x;
std::cout << "Reference Example:" << std::endl;
std::cout << "Value of x: " << x << std::endl;
std::cout << "Value of ref: " << ref << std::endl;
ref = 20;
std::cout << "New value of x: " << x << std::endl;
}
void pointerArithmeticExample() {
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
std::cout << "Pointer Arithmetic Example:" << std::endl;
for (int i = 0; i < 5; i++) {
std::cout << "Element " << i << ": " << *ptr << std::endl;
ptr++;
}
}
void dynamicMemoryExample() {
int* ptr = new int(10); // dynamically allocate memory and initialize to 10
std::cout << "Dynamic Memory Example:" << std::endl;
std::cout << "Value: " << *ptr << std::endl;
delete ptr; // free the allocated memory
}
void functionPointerExample(int* ptr) {
std::cout << "Function Pointer Example:" << std::endl;
std::cout << "Value at ptr: " << *ptr << std::endl;
}
void functionReferenceExample(int& ref) {
std::cout << "Function Reference Example:" << std::endl;
std::cout << "Value of ref: " << ref << std::endl;
}
int main() {
pointerExample();
std::cout << std::endl;
referenceExample();
std::cout << std::endl;
pointerArithmeticExample();
std::cout << std::endl;
dynamicMemoryExample();
std::cout << std::endl;
int x = 30;
functionPointerExample(&x);
std::cout << std::endl;
functionReferenceExample(x);
std::cout << std::endl;
return 0;
}
This example covers the primary usage of pointers and references, including pointer arithmetic, dynamic memory allocation, and passing pointers and references to functions.