Pointer And Arrays
- Siddharth Sharma
- Nov 7, 2024
- 3 min read
Pointers and arrays are closely related in C and C++. Arrays are essentially blocks of contiguous memory, and pointers can be used to access and manipulate these blocks efficiently. In many ways, arrays and pointers can be used interchangeably, but there are some differences in behavior and functionality.
Relationship Between Pointers and Arrays
Array Name as a Pointer: The name of an array (e.g., arr) can be used as a constant pointer to the first element of the array. For example, int arr[5]; means arr is equivalent to &arr[0].
Pointer Arithmetic: You can perform pointer arithmetic to navigate through array elements, which makes accessing elements easier and often more efficient.
Difference in Declaration: An array is declared with square brackets (e.g., int arr[5];), while a pointer is declared with an asterisk (e.g., int* ptr;). Arrays have fixed sizes, while pointers can be reassigned to point to different blocks of memory.
Accessing Array Elements with Pointers
You can access and manipulate array elements using pointers instead of array indexing.
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int* ptr = arr; // Pointer to the first element of arr
// Access elements using pointer arithmetic
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, *(ptr + i)); // *(ptr + i) gives the value at arr[i]
}
// Modify elements using pointer
*(ptr + 2) = 100; // Changes arr[2] to 100
printf("Modified arr[2]: %d\n", arr[2]); // Outputs 100
return 0;
}Explanation:
int* ptr = arr; initializes ptr to point to the first element of arr (i.e., arr[0]).
*(ptr + i) accesses each element of the array using pointer arithmetic.
*(ptr + 2) = 100; modifies the value of arr[2] by dereferencing the pointer at ptr + 2.
Output:
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50
Modified arr[2]: 100Pointer Arithmetic in Arrays
When you add an integer to a pointer, it moves forward in memory by the size of the data type it points to (e.g., 4 bytes for int). Thus, ptr + 1 moves to the next integer in the array, which is arr[1], and so forth.
Arrays of Pointers
You can also have arrays of pointers. This is useful for handling strings or dynamic arrays of data.
#include <stdio.h>
int main() {
const char* names[] = {"Alice", "Bob", "Charlie"};
for (int i = 0; i < 3; i++) {
printf("Name[%d]: %s\n", i, names[i]); // Each element is a pointer to a string
}
return 0;
}Difference Between Arrays and Pointers
Fixed vs. Flexible: Arrays have a fixed size and cannot be resized. Pointers can be reassigned to different memory locations.
Memory Allocation: Arrays have memory allocated for their elements when declared, while pointers are usually uninitialized until they are assigned an address.
sizeof Operator: sizeof(arr) returns the total size of the array, while sizeof(ptr) only returns the size of the pointer.
Example: Difference in sizeof
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int* ptr = arr;
printf("Size of arr: %lu\n", sizeof(arr)); // Outputs size of the entire array (5 * size of int)
printf("Size of ptr: %lu\n", sizeof(ptr)); // Outputs size of pointer, typically 8 bytes on 64-bit systems
return 0;
}Output:
Size of arr: 20 // (5 * 4 bytes, assuming int is 4 bytes)
Size of ptr: 8 // (size of the pointer itself, not the array it points to)Summary
Arrays and pointers are closely related; the array name is a pointer to its first element.
Pointer arithmetic allows efficient traversal of array elements.
Arrays of pointers are helpful for managing strings and lists of variable-length data.
Differences between arrays and pointers include size flexibility, memory allocation, and behavior with sizeof.




Comments