You can also utilize pointers for accessing arrays.
Take into account the subsequent array of integers:
int myNumbers[4] = {25, 50, 75, 100}; |
From the arrays chapter, you gained knowledge that looping through the array elements can be achieved with a for loop.
int myNumbers[4] = {25, 50, 75, 100}; int i; for (i = 0; i < 4; i++) { |
Result:
|
Rather than outputting the value of each array element, let’s print the memory address of each array element.
int myNumbers[4] = {25, 50, 75, 100}; int i; for (i = 0; i < 4; i++) { |
Result:
|
Notice that the last digit of each element’s memory address differs, increasing by 4.
This discrepancy arises due to the typical size of an int type being 4 bytes; recall:
// Create an int variable int myInt; // Get the memory size of an int |
Result:
4 |
As evidenced by the “memory address example” provided above, the compiler allocates 4 bytes of memory for each array element. Consequently, the entire array occupies 16 bytes (4 * 4) of memory storage.
int myNumbers[4] = {25, 50, 75, 100};
// Get the size of the myNumbers array |
Result:
16 |
In C, the relationship between pointers and arrays is notable. Specifically, the name of an array effectively serves as a pointer to the first element of that array.
Feeling perplexed? Let’s delve deeper into this concept using our aforementioned “memory address example” once more.
The memory address of the initial element coincides with the name of the array:
int myNumbers[4] = {25, 50, 75, 100};
// Get the memory address of the myNumbers array // Get the memory address of the first array element |
Result:
|
Essentially, this implies that we can manipulate arrays using pointers!
How? Given that “myNumbers” serves as a pointer to the first element in the array “myNumbers”, you can utilize the * operator to access it:
int myNumbers[4] = {25, 50, 75, 100};
// Get the value of the first element in myNumbers |
Result:
25 |
To access the remaining elements in “myNumbers”, you can increment the pointer/array (+1, +2, etc.):
int myNumbers[4] = {25, 50, 75, 100};
// Get the value of the second element in myNumbers // Get the value of the third element in myNumbers // and so on.. |
Result:
|
Alternatively, you can traverse it with a loop.
int myNumbers[4] = {25, 50, 75, 100}; int *ptr = myNumbers; int i; for (i = 0; i < 4; i++) { |
Result:
|
It’s also viable to modify the values of array elements using pointers.
int myNumbers[4] = {25, 50, 75, 100};
// Change the value of the first element to 13 // Change the value of the second element to 17 // Get the value of the first element // Get the value of the second element |
Result:
|
This approach to handling arrays might appear somewhat intricate, especially with straightforward arrays like those in the examples provided above. However, for larger arrays, leveraging pointers can significantly enhance efficiency in accessing and manipulating array elements. Accessing two-dimensional arrays with pointers is generally regarded as faster and more straightforward. Moreover, given that strings are essentially arrays, pointers can also be employed to access strings. It’s beneficial to understand how this mechanism functions at the moment. However, as emphasized in the preceding chapter, caution must be exercised when dealing with pointers, as unintended overwriting of other data stored in memory is a possibility. |