Pointers & Arrays
Understanding the relationship between pointers and arrays
🔗 Pointers and Arrays Connection
Arrays and pointers are closely related in C. Array names act as pointers to the first element, enabling efficient array manipulation.
// Array name is a pointer to first element
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr; // Same as &arr[0]
printf("First element: %d", *ptr);
Array-Pointer Relationships
Array Name
Points to first element
int arr[5];
// arr == &arr[0]
Array Indexing
Using pointers for access
arr[i] == *(arr + i)
Pointer Arithmetic
Navigate through arrays
ptr++; // Next element
ptr += 2; // Skip 2 elements
Array Traversal
Loop through with pointers
for(ptr = arr; ptr < arr+5; ptr++)
🔹 Array Access Methods
C provides multiple syntactically equivalent methods to access array elements using pointers and subscripts. Both arr[i] and *(arr + i) access the same element, demonstrating that arrays are fundamentally pointers in C. You can also use pointer notation like ptr[i] when ptr points to an array. Understanding these equivalent access methods deepens your comprehension of how arrays work internally and enables you to write more efficient code. Mastering array access techniques improves your ability to manipulate memory directly.
#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int *ptr = numbers;
// Method 1: Array indexing
printf("numbers[2] = %d\n", numbers[2]);
// Method 2: Pointer arithmetic
printf("*(numbers + 2) = %d\n", *(numbers + 2));
// Method 3: Pointer indexing
printf("ptr[2] = %d\n", ptr[2]);
// Method 4: Pointer dereferencing
printf("*(ptr + 2) = %d\n", *(ptr + 2));
return 0;
}
Output:
numbers[2] = 30 *(numbers + 2) = 30 ptr[2] = 30 *(ptr + 2) = 30
🔹 Array Traversal with Pointers
Pointer-based array traversal offers performance benefits and enables efficient iteration through large datasets. Instead of using index variables and repeated array lookups, you can increment pointers to move through array elements. The approach for (int *p = arr; p < arr + size; p++) directly accesses contiguous memory locations. This technique reduces computational overhead and improves cache locality. Pointer traversal is particularly valuable when processing large arrays or when performance is critical in embedded systems and real-time applications.
#include <stdio.h>
int main() {
int scores[] = {85, 92, 78, 96, 88};
int size = sizeof(scores) / sizeof(scores[0]);
int *ptr;
printf("Array elements using pointer:\n");
for (ptr = scores; ptr < scores + size; ptr++) {
printf("Element: %d, Address: %p\n", *ptr, ptr);
}
return 0;
}
Output:
Array elements using pointer: Element: 85, Address: 0x7fff5fbff6a0 Element: 92, Address: 0x7fff5fbff6a4 Element: 78, Address: 0x7fff5fbff6a8 Element: 96, Address: 0x7fff5fbff6ac Element: 88, Address: 0x7fff5fbff6b0
🔹 String Arrays and Pointers
String arrays represent collections of text using pointer arrays, where each pointer references a separate string. Declaration like char *strings[] = {"hello", "world"} creates an efficient way to manage multiple strings without fixed memory allocation. This approach allows flexible string storage and easy manipulation of string collections. Understanding string arrays is crucial for processing text data, implementing command-line arguments, and building text processing utilities. Pointers enable dynamic string management and efficient memory usage.
#include <stdio.h>
int main() {
char *fruits[] = {"Apple", "Banana", "Orange", "Grape"};
char **ptr = fruits; // Pointer to pointer
int i;
printf("Fruits list:\n");
for (i = 0; i < 4; i++) {
printf("%d. %s\n", i + 1, *(ptr + i));
}
return 0;
}
Output:
Fruits list: 1. Apple 2. Banana 3. Orange 4. Grape