DEV Community

Cover image for Pointer 2 : {a,r,r,a,y} Pointers🫵and ➖Pointers Arithmetic ➕
Ridwanur Rahman
Ridwanur Rahman

Posted on

Pointer 2 : {a,r,r,a,y} Pointers🫵and ➖Pointers Arithmetic ➕

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

The output will be

Image description

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
OUTPUT

Image description

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

OUTPUT

Image description

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

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

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

Image description

OUTPUT

Image description

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

OUTPUT

Image description

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

OUTPUT
Image description

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

OUTPUT
Image description

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

OUTPUT

Image description

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

OUTPUT

Image description

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

Image description

OUTPUT

Image description

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

OUTPUT

Image description

POINTER COMPARISION

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

Image description

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

Top comments (0)