Pointers are powerful tools in C programming, and understanding how to perform arithmetic on them is crucial for working with arrays, dynamic memory, and low-level programming. These operations could be simple increment, decrement or addition and subtraction operations. Arithmetic operations on addresses aren’t as simple as adding the number directly to the address, we also need to consider the size of data types.
Example
int pointer Let us take an example of integer pointer pointing to an array of size 2 (a[2]). Originally the pointer is at a[0] i.e. the base address of the address.
Now to access the a[1] element, there is a need to increment the offset by 1. But, if we add 1 to the pointer
00 + 01 = 01, it would now point to an address within a[0] itself.
The correct method would be to multiply the offset by the size of integer i.e. 4.
00 + 01 * 4 = 04Address 04 is the starting address of element a[1]. Hence, we successfully incremented the pointer to a valid memory location as per the data type.
Note: These complex considerations are automatically handled by the compiler. So we don’t need to manually multiply every offset by size of datatype.
Arithmetic Operations
With the working clear, we can look at all possible operations on pointers. There are mainly 5 types of arithmetic operations on pointers, namely:
- 1. Increment and Decrement of a Pointer
- 2. Adding an Integer to a Pointer
- 3. Subtracting an Integer from a Pointer
- 4. Subtracting Two Pointers
- 5. Pointer Comparison
1. Increment and Decrement of a Pointer
-
Increment (
p++): Moves the pointer to the next memory location based on the data type size.Example: If an
intpointer stores address1000andsizeof(int) = 4, afterp++, the address becomes1004. -
Decrement (
p--): Moves the pointer to the previous memory location.Example: If
p = 1000, then afterp--,p = 996.
int a = 22;
int *p = &a;
printf("p = %u\n", p);
p++;
printf("p++ = %u\n", p);
p--;
printf("p-- = %u\n", p); Note: %u is used here to show the address as an unsigned integer, though %p is preferred for pointer output.
2. Adding an Integer to a Pointer
When an integer is added to a pointer, it moves forward by n * size of data type.
Example:
If ptr is an int* at address 1000, then ptr + 5 will point to 1000 + 4*5 = 1020.
int *ptr = &N;
ptr = ptr + 3;3. Subtracting an Integer from a Pointer
Similar to addition, subtracting an integer from a pointer moves it backward in memory.
Example:
int *ptr = &N;
ptr = ptr - 3;4. Subtracting Two Pointers
Subtracting two pointers of the same type gives the number of elements between them.
Example:
int *ptr1 = &N;
int *ptr2 = &x;
int diff = ptr1 - ptr2; // Result is in terms of number of elementsIf ptr1 = 1004, ptr2 = 1000, and sizeof(int) = 4, then diff = 1.
5. Pointer Comparison
Pointers can be compared using relational operators like ==, !=, <, >, etc.
Example:
int arr[5];
int *ptr1 = &arr;
int *ptr2 = &arr[0];
if (ptr1 == ptr2) {
printf("Pointers are equal.\n");
}Comparison with NULL
Pointers can be set to NULL to indicate they don’t point to any memory location.
int *ptr = NULL;
if (ptr == NULL) {
printf("The pointer is NULL.\n");
}

