Why Array Index Starts with 0 , A pointers magic !
Tags: programming, c, beginners, tutorial
Published: 2022-10-25 13:08:52

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. 

```c
#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;
}
```

## 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
```bash
Initial Address 0x7ffc1c122f1c
After Adding 1 0x7ffc1c122f20
Value 10 
```

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

![Pointers & Values](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kl0rr1sntehtel5xs2mm.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cpnapc6tk9t4npnszu8v.gif)

So what I am trying to say here is 
```bash
0x7ffcb63b6e7c + 0 = 140720779439900
0x7ffcb63b6e7c + 1 = 140720779439904
```

## 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
```c
#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
```

I will change the above code with the help of pointers.
```c
#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
```
## 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)
```bash
 * (0x7ffcb63b6e7c + 0) = *(140720779439900) = 1 
 * (0x7ffcb63b6e7c + 1) = *(140720779439904) = 2 
 * (0x7ffcb63b6e7c + 2) = *(140720779439908) = 3 

```

To be more precise this is what happens ,
**arr[ind] = *(arr + ind)**
```bash
#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
```



_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 🥛](https://www.buymeacoffee.com/shriharimohan). Small support comes a long way!

Subscribe If you want to receive these blogs in your mail from @[Medium](https://medium.com/@shriharimohan) for free!

## Try Our new product for free!
[DocsAI](https://docsai.app/) - 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](https://docsai.app/pricing) Just contact me for personalized pricing !

![docsAi](https://firebasestorage.googleapis.com/v0/b/shrihari-personal-storage.appspot.com/o/banner-docs-jpeg.jpeg?alt=media&token=45c47666-de2a-4e02-8881-beb6e43ce581)

## More Free Articles From me 

{% embed https://dev.to/shrihari/the-chrome-dev-tool-you-are-missing-feat-recorder-4ao5 %}

{% embed https://dev.to/shrihari/two-things-that-make-my-terminal-experience-awesome-in-mac-3eie %}

{% embed https://dev.to/shrihari/colors-design-inspiration-websites-for-new-comers-1lcb %}