Linux C Programming Tutorial Part 20 - Pointer address arithmetic

In our previous tutorial in this ongoing C Programming series, we discussed pointers in a bit of detail. Extending that discussion, here we are with some more discussion worthy concepts related to pointers. So without further ado, let's begin.

Pointer address arithmetic in C

Firstly, let's focus on address arithmetic when dealing with pointers. Like we discussed in our previous tutorial if you have a pointer pointing to an array (let's say the beginning of it), it's very easy to access the elements of that array. Here's an example:

#include <stdio.h>

int main()
{
char arr[]={'a','b','c','d'};
int a[]={1,2,3,4};

char *ptr = arr;
int *p = a;

for(int i=0; i<sizeof(arr); i++)
{
printf("\n Character pointer pointing to %u, with value %c", ptr, *ptr);
ptr++;
}

for(int i=0; i<(sizeof(a)/sizeof(int)); i++)
{
printf("\n Integer pointer pointing to %u, with value %d", p, *p);
p++;
}

return 0;
}

So in this code, we have two arrays - 'arr' and 'a'. While the first one is a character array, the second one is an integer array. Then we have to pointers pointing to these arrays - 'ptr' and 'p', respectively. Then there are a couple of 'for' loops wherein we print current address the pointer is pointing to and the value contained at that address.

Here's the output of this code on my machine:

 Character pointer pointing to 726409312, with value a 
Character pointer pointing to 726409313, with value b
Character pointer pointing to 726409314, with value c
Character pointer pointing to 726409315, with value d
Integer pointer pointing to 726409296, with value 1
Integer pointer pointing to 726409300, with value 2
Integer pointer pointing to 726409304, with value 3
Integer pointer pointing to 726409308, with value 4

Now, what's noteworthy here is the addresses printed in the output. For character array, adding '1' to the pointer incremented it to the very next address, but for an integer array, adding '1' to the pointer made it jump 4 addresses ahead. Why is that so?

Well, it all depends on the type of pointer. A character pointer always jumps the number of bytes a character occupies, which is usually 1. Similarly, a pointer to an integer usually jumps 4 bytes when incremented by 1. So this should explain the difference in character and integer pointer jumps in the above output.

Moving on with the pointer arithmetic, pointers can be used in comparison expressions in select cases. For example, if you want to confirm whether or not a pointer 'ptr' points to an element of an array 'arr' (sized 'size'), then you can do the comparison in the following way:

if((ptr >= arr) && (ptr < (arr + size))) 

So effectively, the above expression checks whether or not the address held by the pointer is of one of its elements.

Also, if there are multiple pointers pointing to elements of the same array, you can use the following comparison operators  ==, !=, <, and >=. In fact, you can also perform pointer subtraction in these cases. Here's an example:

#include <stdio.h>

int main()
{
char arr[]= "Welcome to HowtoForge";

char *ptr = arr;
char *p = arr;

while(*p != '\0')
{
p++;
}

printf("\n Length of the string is %d", (p - ptr));

return 0;
}

So in this program, we made sure one pointer points to the first element of the array, while the second pointer is continuously incremented until it points to the last element (which is '\0' in constant strings).

Then, given the fact that we're dealing with characters here (which occupy one byte each), by subtracting the address pointed by first pointer from that pointed by the second pointer, we could find the number of characters, which if you see is the exact length of the string.

NOTE: Keep in mind that operations like addition, multiplication, division, and more are invalid when it comes to pointers. Also, the kind of acceptable operations we've listed so far are also only valid when pointers are of same type and are pointing to elements of the same array.

Conclusion

After discussing basic pointer concepts in the previous tutorial, we built up on the pointers concept by discussing the pointer address arithmetic here. We suggest you practice what all we discussed here on your machine in the form of programs. And in case you of any doubt or query, don't hesitate to leave a comment below.

Share this page:

Suggested articles

0 Comment(s)

Add comment