Arrays and String Manipulation in C Programming
One-dimensional Array Internals
Arrays and Memory
You've likely used arrays to store lists of data, like a series of sensor readings or a collection of user scores. But how does the computer actually handle them? The key is that arrays are stored in contiguous memory. This means all the elements of an array live side-by-side in memory, one after the other, with no gaps.
Think of your computer's memory as a gigantic, numbered street of houses. When you declare an array like int scores[5];, you're asking the system to find five adjacent, empty houses and reserve them for your integers. The address of the first house is known as the of the array.
Because memory is contiguous, we can calculate the address of any element if we know the base address. C's indexing starts at 0 for a very practical reason related to this calculation. The index isn't just a position; it's an offset from the start.
For
scores[3], the address is . The zero-based index makes the math simple and direct. The first element,scores[0], has an offset of zero, so its address is just the base address.
Arrays and Pointers
In C, the name of an array acts as a pointer to its first element. This is a fundamental and powerful concept. When you use the array's name without any square brackets, you are getting its base address. This means you can use pointer arithmetic to access elements.
Let's see this in action. The expression scores gives the base address, while *scores dereferences that address to get the value of the first element, which is the same as scores[0].
#include <stdio.h>
int main() {
int scores[5] = {94, 87, 100, 72, 91};
// 'scores' is the address of the first element.
printf("Base address: %p\n", scores);
// '&scores[0]' is also the address of the first element.
printf("Address of scores[0]: %p\n", &scores[0]);
// Accessing the first element using pointer dereferencing
printf("First element value: %d\n", *scores);
// Accessing the third element (index 2)
// scores + 2 calculates the address of the third element
printf("Third element value: %d\n", *(scores + 2));
printf("Value of scores[2]: %d\n", scores[2]);
return 0;
}
Notice that *(scores + i) is equivalent to scores[i]. The square bracket notation [] is just a more convenient way to write the same pointer arithmetic. It automatically handles the multiplication by the data type size for you. This close relationship between arrays and pointers is a defining feature of C.
When you pass an array to a function, you aren't sending a copy of the entire array. That would be inefficient, especially for large arrays. Instead, you're passing its base address. This is known as call by reference (or more accurately, call by value where the value is an address).
Because the function receives the actual memory address, any modifications it makes to the array elements are permanent. They affect the original array in the calling function. There's no need to return the array; the changes are made directly in memory.
#include <stdio.h>
// The function receives a pointer to the start of the array.
// Note: void print_and_double(int arr[], int size) is equivalent.
void print_and_double(int *arr, int size) {
printf("Inside function before doubling:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
arr[i] = arr[i] * 2; // This modifies the original array
}
printf("\n");
}
int main() {
int my_data[4] = {10, 20, 30, 40};
int data_size = 4;
printf("Original array in main:\n");
for (int i = 0; i < data_size; i++) {
printf("%d ", my_data[i]);
}
printf("\n\n");
// Pass the array (its base address) to the function.
print_and_double(my_data, data_size);
printf("\nArray in main after function call:\n");
for (int i = 0; i < data_size; i++) {
printf("%d ", my_data[i]);
}
printf("\n");
return 0;
}
The output of this program shows that the changes made inside print_and_double persist after the function returns. Understanding this behaviour is crucial for writing correct and efficient C code that works with collections of data.
What is the defining characteristic of how array elements are stored in computer memory?
In C, if scores is an array, what does the expression scores evaluate to?
