# Pointer Arithmetic in C Programming

We can perform arithmetic operations on pointer variable just as you can a numeric value. As we know that, a pointer in C is a variable which is used to store the memory address which is a numeric value. The arithmetic operations on pointer variable effects the memory address pointed by pointer.

Valid Pointer Arithmetic Operations
• Adding a number to pointer.
• Subtracting a number form a pointer.
• Incrementing a pointer.
• Decrementing a pointer.
• Subtracting two pointers.
• Comparison on two pointers.
Invalid Pointer Arithmetic Operations
• Addition of two pointers.
• Division of two pointers.
• Multiplication of two pointers.

## Incrementing a Pointer

Let ptr be an integer pointer which points to the memory location 5000 and size of an integer variable is 32-bit(4 bytes). Now, when we increment pointer ptr

```    ptr++;
```

it will point to memory location 5004 because it will jump to the next integer location which is 4 bytes next to the current location. Incrementing a pointer is not same as incrementing an integer value. On incrementing, a pointer will point to the memory location after skipping N bytes, where N is the size of the data type(in this case it is 4).

```ptr++ is equivalent to ptr + (sizeof(pointer_data_type)).
```
"Incrementing a pointer increases its value by the number of bytes of its data type"

A character(1 bytes) pointer on increment jumps 1 bytes.
An integer(4 bytes) pointer on increment jumps 4 bytes.

This is a very important feature of pointer arithmetic operations which we will use in array traversal using pointers.

## Decrementing a Pointer

Similarly, Decrementing a pointer will decrease its value by the number of bytes of its data type. Hence, after

```    ptr--;
```
ptr will point to 4996.
```ptr--; is equivalent to ptr - (sizeof(pointer_data_type)).
```

## Adding Numbers to Pointers

Adding a number N to a pointer leads the pointer to a new location after skipping N times size of data type.

```ptr + N =  ptr + (N * sizeof(pointer_data_ype))
```

For example, Let ptr be a 4-byte integer pointer, initially pointing to location 5000.
Then ptr + 5 = 5000 + 4*5 = 5020. Pointer ptr will now point at memory address 5020.

## Subtracting Numbers from Pointers

Subtracting a number N from a pointers is similar to adding a number except in Subtraction the new location will be before current location by N times size of data type.

`ptr - N =  ptr - (N * sizeof(pointer_data_ype))`

For example, Let ptr be a 6-byte double pointer, initially pointing to location 5000.
Then ptr - 3 = 5000 - 6*3 = 4982. Pointer ptr will now point at memory address 4982.

## Subtracting Pointers

The difference between two pointer returns indicates “How apart the two Pointers are. It gives the total number of elements between two pointers.
For example, Let size of integer is 4 bytes. If an integer pointer 'ptr1' points at memory location 10000 and integer pointer 'ptr' points at memory location 10008, the result of ptr2 - ptr1 is 2.

## C program to show pointer arithmetic

```#include<stdio.h>

void main() {
int int_var = 10, *int_ptr;
char char_var = 'A', *char_ptr;
float float_val = 4.65, *float_ptr;

/* Initialize pointers */
int_ptr = &int_var;
char_ptr = &char_var;
float_ptr = &float_val;

printf("Address of int_var = %u\n", int_ptr);
printf("Address of char_var = %u\n", char_ptr);
printf("Address of float_var = %u\n\n", float_ptr);

/* Incrementing pointers */
int_ptr++;
char_ptr++;
float_ptr++;
printf("After increment address in int_ptr = %u\n",
int_ptr);
printf("After increment address in char_ptr = %u\n",
char_ptr);
printf("After increment address in float_ptr = %u\n\n",
float_ptr);

/* Adding 2 to pointers */
int_ptr = int_ptr + 2;
char_ptr = char_ptr + 2;
float_ptr = float_ptr + 2;

printf("After addition address in int_ptr = %u\n",
int_ptr);
printf("After addition address in char_ptr = %u\n",
char_ptr);
printf("After addition address in float_ptr = %u\n\n",
float_ptr);

return 0;
}
```
Output
```Address of int_var = 2293300
Address of char_var = 2293299
Address of float_var = 2293292

After increment address in int_ptr = 2293304
After increment address in char_ptr = 2293300
After increment address in float_ptr = 2293296

After addition address in int_ptr = 2293312
After addition address in char_ptr = 2293302
After addition address in float_ptr = 2293304
```

## Arrays of Pointers in C

Array of pointers in C programming language can be declared like an array of integers or character. An array of pointers is a collection of pointer variables stored in continuous memory location. In other works, it is an array of addresses. Each element of a pointer array is bound to store address of same data type. Here is the syntax of declaring an array of pointers.

```data_type *identifier[size];
```
For Example
```int *array_ptr[100];
```

Array 'array_ptr' is an array of pointer of size 100, where each array element can store the address of integer variable. All array arithmetic holds good with array or pointers also. Here is an example of array of integer pointers.

## C Program to show the use of Array of Pointers

```#include <stdio.h>

void main() {
int *ptr_array[6];
int a1=1, a2=2, a3=3 ,a4=4, i;
int int_array[] = {10, 20};

/* Assigning addresses of individual variables */
ptr_array[0] = &a1;
ptr_array[1] = &a2;
ptr_array[2] = &a3;
ptr_array[3] = &a4;

/* Assigning addresses of integer array elements */
ptr_array[4] = &int_array[0];
ptr_array[5] = &int_array[1];

for(i=0; i<6; i++) {
printf("%d\n", *(ptr_array[i]));
}

return 0;
}
```
Output
```1
2
3
4
10
20
```

## Arrays and Pointers in C

• Relationship Between Arrays and Pointers : In C, arrays and pointers are closely related. An array name, when used in an expression, is automatically converted to a pointer to the first element of the array.
```int numbers[] = {1, 2, 3, 4, 5};
// Array name 'numbers' is automatically
// converted to a pointer
int *ptr = numbers;
```
Now, ptr points to the first element of the numbers array.

• Accessing Array Elements with Pointers : Array elements can be accessed using pointers and pointer arithmetic.
```// Accesses the second element using pointer arithmetic
int secondElement = *(ptr + 1);
```

• Iterating Through Arrays with Pointers : Pointers are commonly used for iterating through arrays.
```int *current = numbers;
for (int i = 0; i < 5; ++i) {
printf("%d ", *current);
current++;
}
```
This loop prints each element of the numbers array using a pointer.

• Pointers as Function Parameters : When passing an array to a function, what gets passed is a pointer to the first element of the array.
```void printArray(int *arr, int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", arr[i]);
}
}

int numbers[] = {1, 2, 3, 4, 5};
// Passes a pointer to the numbers array
printArray(numbers, 5);
```

• Pointers and Dynamic Memory Allocation : Pointers are essential when working with dynamically allocated memory. The malloc function returns a pointer to the allocated memory.
```int *dynamicArray = (int *)malloc(5 * sizeof(int));
```

### Conclusion

A comprehension of pointer arithmetic is critical for performing operations such as dynamic memory allocation, traversing arrays, and manipulating data efficiently. The software facilitates the traversal of intricate data structures, accessing array elements, and iterating over memory locations.

Nonetheless, this authority entails accountability. It is imperative for developers to exercise prudence in order to prevent frequent errors linked to pointer arithmetic, including exceeding the bounds of memory access and inducing undefined behavior. Writing robust and error-free C code requires strict adherence to best practices, which includes proper initialization, bounds checking, and avoiding the exploitation of pointers.