# Increment and Decrement 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
```

#### ExplanationIn 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 doubtExample 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;		// Let's have the address of a 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 = 3872619352
Value of a = 30