Introduction:
Operations that can be performed on Addresses:
WKT pointer variable will hold the address of another variable.
i.e
int a = 10;
int *ptr = NULL;
ptr = &a;
Now,
If you print “ptr”, it will print the value that is stored inside it. i.e the address of variable a.
If you print “*ptr”, it will print the value present inside the address. i.e the value of variable a.
If you print “&ptr”, it will print the address of pointer variable “ptr”
So on Address we can only perform Subtraction, Increment and Decrement. All other operations are invalid.
Valid Operations:
ptr2 - ptr1
ptr1 ++
ptr1 --
Invalid Operations:
ptr2 * ptr1
ptr2 + ptr1
ptr2 / ptr1
We also know below operators:
* Used to declare a pointer
* Used to dereference a pointer
-> Used to access fields of a structure referenced by a pointer
(data type) Used to change the type of pointer.
We shall understand more in-depth with a series of examples.
Operations that can be performed on values:
On values we can perform Subtraction, Addition, Multiplication, Increment, Decrement, Division.
Valid Operations:
*ptr2 - *ptr1
*ptr1 ++
*ptr1 --
*ptr2 * *ptr1
*ptr2 + *ptr1
(*ptr2) / (*ptr1)
While division, it is important to include inside braces. Because, if you write *ptr2 / *ptr1, “/* ptr1” “/*” is starting of multi-line comment syntax, and compiler will give error.
Now, let us understand Pointer Arithmetic with series of example.
Incrementing a Pointer in C
You can increment a pointer as below:
ptr++
or
ptr = ptr + 1;
So what happens when you increment a pointer?
When you do ptr ++, it will move to the next block of the size of pointer variable.
i.e new_address = current_address + (i * size_of(data_type))
Example:
If suppose ptr = 2000; i.e if “ptr” is holding the address 2000 and is a type int pointer, when you do ptr++, it will move 4 bytes ahead and now ptr will be pointing to “2004”.
Example 1: Simple example to check how many blocks new pointer will move:
#include<stdio.h>
int main ()
{
int num = 50;
int *ptr = #
printf("The address hold by ptr = %u\n", ptr );
ptr++;
printf("The new address hold by ptr = %u\n", ptr );
return 0;
}
Output:
The address hold by ptr = 3819903544
The new address hold by ptr = 3819903548
As you can see in above example, the pointer has jumped 4 bytes that is the size of an integer variable.
Example 2: Pointer incrementing using arrays as example:
#include<stdio.h>
int main ()
{
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
int i = 0;
printf("The array elements are : \n");
for ( i = 0; i < 5; ++i)
{
printf("The address is = %u value is = %d \n", ptr, *ptr );
ptr++;
}
return 0;
}
The array elements are :
The address is = 3859409440 value is = 1
The address is = 3859409444 value is = 2
The address is = 3859409448 value is = 3
The address is = 3859409452 value is = 4
The address is = 3859409456 value is = 5
Decrementing/ Subtracting a Pointer in C
You can decrement a pointer as below:
ptr--
or
ptr = ptr - 1;
So what happens when you decrement a pointer?
When you do ptr –, it will move back to the next block of the size of pointer variable.
i.e new_address = current_address – (i * size_of(data_type))
Example:
If suppose ptr = 2000; i.e if “ptr” is holding the address 2000 and is a type int pointer, when you do ptr–, it will move 4 bytes back and now ptr will be pointing to “1996”.
Example 1: Simple example to check how many blocks new pointer will move when pointer decrement.
#include<stdio.h>
int main ()
{
int num = 50;
int *ptr = #
printf("The address hold by ptr = %u\n", ptr );
ptr--;
printf("The new address hold by ptr = %u\n", ptr );
return 0;
}
The address hold by ptr = 3798407736
The new address hold by ptr = 3798407732
Example 2: Pointer decrement using arrays as example:
#include<stdio.h>
int main ()
{
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[4];
int i = 0;
printf("The array elements are : \n");
for ( i = 0; i < 5; i++)
{
printf("The address is = %u value is = %d \n", ptr, *ptr );
ptr--;
}
return 0;
}
The array elements are :
The address is = 3844827696 value is = 5
The address is = 3844827692 value is = 4
The address is = 3844827688 value is = 3
The address is = 3844827684 value is = 2
The address is = 3844827680 value is = 1