Welcome to a deep dive into C Pointer Arithmetic! By the end of this lesson, you'll understand how to manipulate and navigate memory using pointers, a powerful tool in C programming. Let's dive in! 🐋
Pointers in C are variables that store memory addresses. They allow us to manipulate data directly in memory, making complex data structures and dynamic memory allocation possible.
What are pointers in C?
C provides four pointer types:
char *: Pointer to a character (used for strings)int *: Pointer to an integerfloat *: Pointer to a floating-point numbervoid *: Pointer to any data type (used for generic pointers)Pointer arithmetic allows us to manipulate the memory addresses that pointers hold. Here's how it works:
Increment (++, +=): Increases the memory address by the size of the pointed type. For example, ptr += 1; increments the pointer by 4 bytes (for an int).
Decrement (--, -=): Decreases the memory address by the size of the pointed type.
Accessing the pointed value: Use the * operator to access the value that the pointer points to. For example, *ptr; accesses the value at the memory address pointed by ptr.
Advanced pointer arithmetic involves pointer arithmetic with arrays and pointer arithmetic with pointers.
Pointer Arithmetic with Arrays: When a pointer points to the first element of an array, incrementing the pointer moves it to the next element. For example, int arr[5] and int *ptr = arr; means ptr points to the first element of arr. ptr++ now points to the second element.
Pointer Arithmetic with Pointers: We can perform arithmetic on pointers that point to pointers, moving between different memory locations. For example, int **ptr = &arr; points to the address of arr. ptr++ now points to the address of arr+1 (the second array).
Let's explore two complete, working examples that showcase pointer arithmetic in C:
Example 1: Pointer Arithmetic with Arrays
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
// Print the initial array
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, *ptr);
ptr++;
}
// Now, let's modify the array using pointers
ptr = arr;
*ptr++ = 100; // Replace arr[0] with 100
printf("\nModified array:\n");
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, *ptr);
ptr++;
}
return 0;
}Example 2: Pointer Arithmetic with Pointers
#include <stdio.h>
int main() {
int arr1[3] = {1, 2, 3};
int arr2[3] = {4, 5, 6};
int **ptr = &arr1;
// Print the initial arrays
for (int i = 0; i < 3; i++) {
printf("arr1[%d] = %d\n", i, **ptr);
printf("arr2[%d] = %d\n", i, **(ptr + 1));
ptr++;
}
// Now, let's modify the arrays using pointers
**ptr = 100; // Replace arr1[0] with 100
**(ptr + 1) = 200; // Replace arr2[0] with 200
printf("\nModified arrays:\n");
for (int i = 0; i < 3; i++) {
printf("arr1[%d] = %d\n", i, **ptr);
printf("arr2[%d] = %d\n", i, **(ptr + 1));
ptr++;
}
return 0;
}What is the output of the following code snippet?
That's it for our deep dive into C Pointer Arithmetic! As you practice and explore, you'll become more comfortable with manipulating memory and building complex data structures in C. Happy coding! 🤓💻