Learn. Spread. Learn.

Pointers in C – Array Implementation

In the previous article, we looked briefly on what pointers are. Furthermore, we learnt a simple implementation of pointers. In this post, I would like to go a bit further and explain you about relationship of pointers with arrays. So, let’s move into pointers in C and its array implementation.

Array Implementation. It can be done by pointers.
An Array of length 10

First of all, we have to understand the memory allocation of array. In an array, we allocate the memory address in a contiguous fashion. In other words, we allocate consecutive memory addresses for the array. If an array is of length 10, then there are 10 blocks of memory assigned and each block size is determined by the data type of the array. For example, an array int arr[5] will allocate 5 * 4 bytes=20 bytes of memory because integer type in C allocates 4 bytes of memory. Had it been a character array, then each block would have allocated 1 byte resulting in 5 bytes array.

Also, the base address of an array is the memory address where its first element resides. This is important for the implementation of array in C with pointers. Simply, the address of arr[0] is the address of the array arr. That is, &arr[0] = &arr = arr. Similarly, the address of second element of the array will be &arr[1] = arr + 1 and so on.

Let’s write a simple program of array to calculate the total sum of the element:

#include <stdio.h>
int sumTotal (int* a, size_t size);
// int sumTotal (int a[], size_t size);  // This is totally equivalent

int main()
{
    // Define an array
    int arr[5] = {1, 2, 3, 4, 5};
    
    // Determine the size of the array using sizeof() operator
    size_t size = sizeof(arr) / sizeof(arr[0]);
    
    // Variable to store the total
    int total;
    
    // Pass the array and its size to the function.
    // Size is the must because we are working with memory locations.
    total = sumTotal (arr, size);
    
    //Print the result
    printf("The total of the elements is: %d\n", total);
    
    return 0;
}

// Array name is equivalent to the base memory address of the Array
// Hence, we can pass it in the function
int sumTotal (int* a, size_t size) {
    int i;
    int sum = 0;
    for (i = 0; i < size; i++) {
        // 
        sum += *(a++);      // We can dereference the pointers and also increment to point to next element
        //sum += *(a + i);    // This is totally equivalent.
        // sum += a[i];     // This is totally equivalent too. 
    }
    return sum;
}

We can see that, there are different methods to derefence an array, which I have shown above. First of all, you can pass the array to the function as arr[] or *arr. Secondly, you can dereference using dereference operator or simple array operator. However, you should be aware of precendence of operators.

I hope you have cleared your doubt on passing array using pointers. If you like to visit my github profile, click here: https://github.com/kriss-u.

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments