Diving into Arrays: Pointers, Dereferencing, and the * Operator

Diving into Arrays: Pointers, Dereferencing, and the * Operator

I used to be totally confused about pointers and arrays, especially when I saw that '*' symbol in the code. However, once I grasped the concept that the array name represents the pointer to the base address, specifically that "array = &array[0]," everything clicked into place and it made my life so much easier! So today, I’m going to break down this whole concept in a simple way, hoping it will be worth your time.

Arrays

Alright, let's start by clarifying a few key concepts about arrays. An array is essentially a collection of data items or elements. For example, in the integer array a below:

int a[ ] = {2, 4, 20, 0, 10};
printf("First element: %d\n", a[0]);

The array a consists of four elements: a[0], a[1], a[2], and a[3]. To access these elements, we use the notation a[0], a[1], and so on.

Additionally, we can obtain the addresses of these elements using the "&" operator, such as &a[0], &a[1], and &a[2]. The format identifier for addresses is %p.

The first address, which is also the base address of the array, is commonly referred to as &a[0].

Pointer Revelation

Now that we've got the array business sorted, it’s time to understand the meaning of a pointer. A pointer is a variable that stores the address of another variable.

Let's break it down with a little example. Say we have this code snippet:

int a[] = {2, 4, 20, 0, 10};
int *q;

q = &a[1];

In this case, "q" is a pointer that stores the address of the second element, a[1].

Array Name Trick

Here's where things get really interesting. The array name is inherently a pointer and points to the base address of the array. The array name becomes a pointer to the first element. We can even prove it by printing out the addresses, and we'll see that both of these printf statements will print the same address.

printf("Base Address of the Array:\n");

printf("Address: %p\n", &a[0]);
printf("Address: %p\n", a);

Now that we've unmasked the array name's secret identity, let's have some fun with pointer arithmetic

By incrementing the array name, we can access subsequent addresses in the array. For example, to obtain the address of a[2], we can use a + 2

Similarly, the following printf statements will yield the same result:

printf("Address of Elements:\n");

printf("Address of a[1]: %p\n", &a[1]);
printf("Address of a[1]: %p\n\n", a+1);

printf("Address of a[2]: %p\n", &a[2]);
printf("Address of a[2]: %p\n\n", a+2);

You get the pattern, right? In general, a + i is equal to &a[i]. By playing with the array name, we can get the addresses of all the elements.

💡
&a[ i ] = a + i

Dereferencing

To access the elements of an array, we use dereferencing. When you dereference a pointer, it gives you the value of the variable it's pointing to. In our case, dereferencing the array name provides us with the value of the first element, *a = a[0].

printf("Value of the first Element:\n");

printf("Value: %d\n", *a);
printf("Value: %d\n", a[0]);

The following pairs of printf statements will yield the same result i.e the value of the array elements

printf("Element values:\n");

printf("Value: %d\n", a[1]);
printf("Value: %d\n\n", *(a + 1));

printf("Value: %d\n", a[2]);
printf("Value: %d\n\n", *(a + 2));

printf("Value: %d\n", a[4]);
printf("Value: %d\n", *(a + 4));

Based on the pattern above, we can conclude that

💡
a [ i ] = * ( a + i )

In summary:

Pointer arithmetic is applicable to arrays, it lets us easily hop between elements. By adding an integer value to the array name, we can retrieve the address of the desired element. This concept is particularly useful when iterating through arrays or manipulating their elements.

Dereferencing the array name or a pointer to an array element using the '*' operator gives us access to the value stored at that particular memory location. This allows us to read or modify array elements using pointers.


It's important to remember that arrays in C are not dynamically resizable. Once you define an array, its size stays fixed. No expanding or shrinking! So be careful not to go wild and try to access elements beyond the boundaries.

Check out the code file I used for this array-pointer dance.

So there you have it! I hope this funny little journey made it all crystal clear for you. See you soon!