Pointer part 2 (Array vs Pointer & Passing Pointer To Function)
Arrays in C++
An array is a collection of elements of the same type stored in contiguous memory locations. In C++, arrays can be of primitive types (int, float, etc.) or user-defined types (structures, classes). Arrays have a fixed size, and the elements are accessed using an index.
int numbers[5] = {1, 2, 3, 4, 5};
Pointers in C++
A pointer is a variable that stores the memory address of another variable. Pointers allow for dynamic memory allocation and manipulation. They are often used for efficient memory management and for accessing elements in arrays.
int x = 10; int *ptr = &x; // ptr now holds the address of x
Array vs. Pointer
In many contexts, arrays and pointers in C++ exhibit similar behavior. When an array name is used in an expression, it decays into a pointer to its first element. For example:
int arr[3] = {1, 2, 3}; int *ptr = arr; // Equivalent to &arr[0]
However, there are differences:
- An array variable cannot be reassigned to point to a different memory location, whereas a pointer can.
- Arrays carry information about their size, while pointers do not inherently know the size of the memory they point to.
- Arrays can be used with the sizeof operator to determine their size, but pointers alone cannot.
Pointer Arithmetic
Pointers can be incremented or decremented to navigate through an array or block of memory.
int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; cout << *ptr; // Prints the first element (1) ptr++; // Moves to the next element cout << *ptr; // Prints the second element (2)
Example 1:
#include <iostream> using namespace std; int main() { int arr[7] = {11,22,33,44,55,66,77}; for (size_t i = 0; i < 7; i++) { cout << *(arr + i) << endl; } return 0; }
code prints the elements of an array using pointer arithmetic.
cout << *(arr + i) << endl;: Utilizes pointer arithmetic to access each element of the array. The expression arr + i calculates the memory address of the i-th element, and *(arr + i) dereferences the pointer to access the value stored at that address.
Output:
If you run this program, the output will be:
11 22 33 44 55 66 77
Example 2:
#include <iostream> using namespace std; int main() { int arr[7] = {11,22,33,44,55,66,77}; int *ptr; ptr = arr; for (size_t i = 0; i < 7; i++) { cout << *ptr << " "; ptr++; } return 0; }
This code accomplishes the same goal as the previous example but uses a pointer (ptr) and pointer arithmetic to iterate through the array.
Output:
If you run this program, the output will be:
11 22 33 44 55 66 77
Passing Pointers to Functions
When passing an array to a function, you are actually passing a pointer to the first element of the array. This is due to the array decaying into a pointer in function arguments.
Example 3:
This code demonstrates the usage of pointers and a function that modifies the value it points to.
#include <iostream> using namespace std; int fun(int *p) { *p = *p + 1; return *p; } int main() { int x = 1; int *ptr = &x; cout << fun(ptr) << endl; cout << x << endl; cout << fun(&x) << endl; cout << x << endl; return 0; }
Key points:
- Function fun:
int fun(int *p): Takes a pointer to an integer as a parameter.
*p = *p + 1;: Increments the value pointed to by the pointer.
return *p;: Returns the updated value. - Main Function:
int x = 1;: Declares and initializes an integer variable x.
int *ptr = &x;: Declares a pointer ptr and assigns the address of x to it.
cout << fun(ptr) << endl;: Calls the function fun with the pointer ptr and prints the result. The value of x is now 2.
cout << x << endl;: Prints the current value of x after the function call. It is now 2.
cout << fun(&x) << endl;: Calls the function fun with the address of x directly and prints the result.
The value of x is now 3.cout << x << endl;: Prints the final value of x. It is now 3.
Output:
If you run this program, the output will be:
2 2 3 3