Join Regular Classroom : Visit ClassroomTech

Increment and Decrement of a Pointer (Pointer Arithmetics) in C

A pointer is an address, means it is a numerical value. Hence, arithmetic operation can be done on a pointer. There are four operations that can be done on a pointer. Those are:

1. Increment (++) and Decrement (- -)
2. Addition of any integer to pointer (+)
3. Subtraction of any integer from pointer (-)
4. Subtracting two pointers of same datatype (-)

Don’t know what is Addition and Subtraction of a Pointer in C? Read: Addition and Subtraction of a Pointer (Pointer Arithmetics) in C

Here, we’ll discuss about Increment and Decrement of pointers.

When a pointer is incremented by 1, actually the pointer increments by the number equal to the size of the data type of the pointer. As an example if we increment a integer type pointer which holds the value of an address say 2000 then when it is incremented, the address becomes 2004 as the size of integer is 4 bytes in 64 bit machine.

Let’s see an example:

Example 1

/* C program to perform Increment of pointer */
/* www.codewindow.in */

#include <stdio.h>  

// Driver code
int main() {  
	int a=5;        
	int *ptr;	// Integer type pointer      
	ptr=&a;		// Stores the address of 'a' variable        
	printf("Before increment address of the variable is: %u", ptr);        
	
	++ptr;		// Incrementing the pointer by 1;  
	
	printf("\nAfter increment address of the variable is: %u", ptr);
	
    return 0;  
}    

Output

Before increment address of the variable is: 612368028
After increment address of the variable is: 612368032

Explanation

In the above example after incrementing the ptr variable by one the ptr variable will incremented by 4 bytes as it is handling a integer type variable of size 4 bytes in a 64 bit machine. This increment operation moved the pointer to the next memory location without affecting the actual value which is 5. You can print and check it.

If the same operation has been done while ptr pointing to a character then after increment it will give current address+1 i.e 612368029 as size of char datatype is 1 byte.


Now let’s talk about Decrement too. It’s quiet similar like increment.

When a pointer is decremented by 1, actually the pointer decreases by the number equal to the size of the datatype of the pointer. As an example if we decrement a integer type pointer which holds the value of an address say 2000 then when it is decreased, the address becomes 1996 as the size of integer is 4 bytes in 64 bit machine.

An example will definitely solve your doubt

Example 2

/* C program to perform Decrement of pointer */
/* www.codewindow.in */

#include <stdio.h>  

// Driver code
int main() {  
	int a=5;        
	int *ptr;	// Integer type pointer      
	ptr=&a;		// Stores the address of 'a' variable        
	printf("Before decrement address of the variable is: %u", ptr);        
	
	--ptr;		// Decrementing the pointer by 1;  
	
	printf("\nAfter decrement address of the variable is: %u", ptr);
	
    return 0;  
}    

Output

Before decrement address of the variable is: 164108828
After decrement address of the variable is: 164108824

Let us take another example of decrement of pointers using an array.

Example 3

/* C program to perform Decrement of pointer using array */
/* www.codewindow.in */

#include <stdio.h>

// Driver code
int main () {
    int  a[] = {10, 20, 30};
   	int  i, *ptr;

   	ptr = &a[2];		// Let's have the address of a[2] in ptr pointer
	
   	for (i = 2; i >= 0; i--) {
		    printf("Address of a[%d] = %u", i, ptr );	// Print address of elements
      		printf("\nValue of a[%d] = %d\n", i, *ptr );	// Print value of that index

      		--ptr;		// Pointer moves to the previous memory location
   	}
	
   	return 0;
}

Output

Address of a[2] = 3872619352
Value of a[2] = 30
Address of a[1] = 3872619348
Value of a[1] = 20
Address of a[0] = 3872619344
Value of a[0] = 10

Explanation

As the pointer is pointing to an array of integers hence the for decrementing the pointer every time the address will be decremented by 4 bytes, as per the size of an integer.

NOTE: The your output address may not match with us even with each execution it can change. But it is guaranteed that the difference between both addresses will be same with us (if you use 64 bit machine)

You Missed

Also Checkout