DEV Community

Shrihari Mohan
Shrihari Mohan

Posted on • Edited on

Why Array Index Starts with 0 , A pointers magic !

The very first language I learnt was C and it took months to understand pointers. I can never forget pointers in my life.

If you know pointers and arrays here is the summary of this blog

  1. arr[] -> arr behaves like a pointer that holds the First element address
  2. arr[0] = * (arr + 0) = * arr, here + is not decimal arithmetic but pointer arithmetic. Thats why index starts with 0.

For more details here you go!
The only thing you should know to understand is

& - means address of
* - means value at that address
%p - Format Specifier used to print address

size of a data type may vary with compiler and operating systems.

We will understand what & and * means with a simple C program.

#include <stdio.h>

int main() {
    int value = 10 ;
    int * addressOfValue = &value;

    printf("Initial Address %p\n", addressOfValue);
    printf("After Adding 1 %p", addressOfValue + 1);
    printf("Value %d", *addressOfValue);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Code Breakdown

  • value is a int variable holding 10
  • The size of the value is 4 bytes
  • addressOfValue is a (int *) type. This means addressOfValue contains address of a integer variable
  • The size of the addressOfValue is 4 bytes ( Pointer Variable size )
  • &value is address of value. We're assigning the address of integer variable to addressOfValue

Output

Initial Address 0x7ffc1c122f1c
After Adding 1 0x7ffc1c122f20
Value 10 
Enter fullscreen mode Exit fullscreen mode

To improve clarity, let's convert the hexadecimal above to decimal.

Pointers & Values

0x7ffcb63b6e7c = 140720779439900
0x7ffcb63b6e80 = 140720779439904

we used value of operator (*) to print the value at the address. That is 10

There is a difference of 4 Bytes. We added 1 to the addressOfValue it added 4 bytes. So here addition is not numbers but size of the value (4 Bytes).

If long int is 8 Bytes. Then addition of 1 to the address will add 8 bytes.

Size Matters

So what I am trying to say here is

0x7ffcb63b6e7c + 0 = 140720779439900
0x7ffcb63b6e7c + 1 = 140720779439904
Enter fullscreen mode Exit fullscreen mode

Array & Pointers Magic

The Below is a simple Array for loop where index starts with 0. You get where I am going .. see the next piece of code

#include <stdio.h>

int main() {
   int arr[] = {1 , 2 , 3 };
   int arrSize = 3;           

   for ( int ind = 0 ; ind < arrSize ; ind ++) {
       printf("%d ", arr[ind]);
   }
   return 0;
}

// Output
// 1 2 3
Enter fullscreen mode Exit fullscreen mode

I will change the above code with the help of pointers.

#include <stdio.h>

int main() {
   int arr[] = {1 , 2 , 3 };
   int arrSize = 3;           

   int * arrPointer = arr;

   for ( int ind = 0 ; ind < arrSize ; ind ++) {
       printf("%d ", * ( arrPointer + ind ) );
   }
   return 0;
}

//Output
//1 2 3
Enter fullscreen mode Exit fullscreen mode

Code Breakdown

  • arrPointer gets the address of arr. Note that we didn't use & operator because arr itself is a special pointer pointing to first element in an array
  • We changed the printf that * ( arrPointer + ind)
 * (0x7ffcb63b6e7c + 0) = *(140720779439900) = 1 
 * (0x7ffcb63b6e7c + 1) = *(140720779439904) = 2 
 * (0x7ffcb63b6e7c + 2) = *(140720779439908) = 3 

Enter fullscreen mode Exit fullscreen mode

To be more precise this is what happens ,
arr[ind] = *(arr + ind)

#include <stdio.h>

int main() {
   int arr[] = {1 , 2 , 3 };
   int arrSize = 3;           

   for ( int ind = 0 ; ind < arrSize ; ind ++) {
       printf("%d ", * ( arr + ind ) );
   }
   return 0;
}

// Output
// 1 2 3
Enter fullscreen mode Exit fullscreen mode

peace🕊


If you are here it means you may have enjoyed reading this blog. Just follow me @shrihari which will motivate to write more, contribute open source.

You can make a drink Buttermilk 🥛. Small support comes a long way!

Subscribe If you want to receive these blogs in your mail from @Medium for free!

Try Our new product for free!

DocsAI - Create AI support agents with your documents in the most affordable price, starts at 0$. Don't need a bot , but need ai help on your docs just upload and start chating !

Using for a company ? Check out our pricing Just contact me for personalized pricing !

docsAi

More Free Articles From me

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas

arr[] -> arr is a special pointer that holds the First element address

No it isn't. A pointer has its own address in memory. The name of an array doesn't, so it is not any kind of pointer. A curious quirk of C is that mentioning the name of an array in most contexts causes the compiler to treat it as if it were a pointer to the first element — but that still doesn't make it a pointer.

Collapse
 
shrihari profile image
Shrihari Mohan

Thanks for making this blog more accurate , I will update and let you know. Yes in almost all contexts it is converted as pointer.