Pointer 2 : {a,r,r,a,y} Pointers🫵and ➖Pointers Arithmetic ➕
Published: 2024-01-21 13:05:15

Previously, we saw the basics of Pointers. Now let us get some ideas on pointers to Array. 

## How are array pointers formed?
In C programming, if we want to access multiple memory locations of the same data type, we can use array pointers. We can simply access the data by **dereferencing** the pointers pointing to it. Suppose we have an array of length 3, and the elements are 1, 2, and 3.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7r6r46yah2rufq5o344n.png)

The **output** will be 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/irvp3pzx3ujxuh1xbj3h.png)

Here, in the above example, we can see the memory location of each variable is different, which is stored in an array.

The syntax of the array pointer is simple.
**_Data_type (*name)[size_of_the_array]_**
_**While naming the variable of the pointer in terms of  array, we must use ().**_
**One thing to note down is that** the pointer that points the 0th element of the array and the pointer that points the whole array are completely different.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3ghxq95z020l99pb5fh.png)
**OUTPUT** 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dyskfzhrv5id97gdhsej.png)

Here, p is a base-type integer, which is 4 bytes, whereas the base-type of parr is the size of the arr which is in this case 20 bytes.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8sei8gdy8r62rcd8t76i.png)


**OUTPUT**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wn2c6jf3ownhgn9zvsx8.png)

So when we are using parr++, the pointer is shifted after 20 bytes. 

## Pointer Arithmetic

## POINTER ADDITION AND SUBSTRACTION
Suppose we have a pointer pt and an array of length 6. Now we want to point to the address of the first element of the array. So the memory address of the first element will be pt = &array[0].


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prglq06tqfpqoxafdfuc.png)


Now suppose we want to find out the memory location of the 4th element of the array. Now we can simply write pt = &arr[3], since the 4th element will be in the 3rd index.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zoxsrhkl3vqvjlds2jpb.png)



There is another way to do this as well, which is by just adding.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1dryg8m5u3cbws1vp6o.png)


**OUTPUT**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8gs279ir47n6dlfdf08.png)

pt = pt + 3 refers to the memory location of the 3rd index of the pointer. Here, (pt = pt+3) is equivalent to (pt = &arr[3]).

Here, what is happening during the pointer addition is that it adds 4 bytes in each increment. And the int type is 4 bytes, so for each increment, the pointer moves to 4 bytes of space.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pb7pkqk1o62kg2zpr8gj.png)


**OUTPUT**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gg6ukykj1pjhd07uu50.png)



_**Pointer subtraction**_ is also similar to addition. In addition, while adding, we were accessing the pointer to the next element of the index after 4 bytes. Whereas in the case of subtraction, we just have to go back 4 bytes to access the previous memory location of the element in the array.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a215y7nbo47p5hcm9tb1.png)



**OUTPUT**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/81a1qd8i7r8723zg2uj2.png)

## Subtraction of two pointer
Suppose we want to know what the increment is between two elements of an array through two pointers. The subtraction of two pointers basically gives the increment between the two pointers.

**Note that the two pointers must be of same type.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xven9c7sx75l7mhkkwp8.png)

**OUTPUT** 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1wqefurgrr176u1pwnx.png)

Here, the increment between the first index and the last index is 5. What's happening here is that suppose the memory location of the 0th index is 1000, so by the 5th index, the memory location will be 1020. And we put the memory location of the 0th index in ptr1 and the 5th index in ptr2. After subtracting ptr1 from ptr2, we get 20, and since the size of the int type is 4 bytes, 20/4 = 5. So the increment is 5.

## Pointer increment and decrement

Let's start with the concept of **post-increment**. Post-increment is such that first the value will be assigned, then the increment will be done.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpct1jrxl0j0cqs8rl38.png)

**OUTPUT**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swt481j7is9tqnlcfnbh.png)

Here, we can see that the first output is 2, which is the first element. Let the base address be 1000. Now what pt++ is doing is first assigning the value of the address 1000, then incrementing it to the next element address, which is 1004, and then printing the value of that address.
Another thing is **pre-increment**. In pre-increment, the value is assigned after the increment is done.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13a0r6zz1v0x4zfk6kq6.png)

**OUTPUT**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqpeqqosy71dauvmqv4t.png)

Similarly, the pointer decrement also happens in this way. It also happens in post-decrement and pre-decrement ways.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fehrc6r6lnuvprf13l2f.png)

**OUTPUT**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8zb6xnp9s2itm36uwgi.png)

Here, the pointer points to the address of the 3rd element, and it first prints the 3rd element, and after that, it decrements and prints the 2nd element.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9fu7j0rudftvlc7g1o6c.png)

**OUTPUT**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h12bs2hrykdmpiffucxj.png)

### POINTER COMPARISION 

In C, we can simply compare two pointers by using and implementing all the operators.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cl1ki5t6rhzausn5ec9v.png)


  
These pointer comparisons can be used to sort an array or find some user input numbers or characters in an array.

### Example
Some application of pointers are given [here](https://github.com/Ridwan805/Pointers/tree/main/2%20Array%20to%20pointer%20and%20pointer%20arithmatic)