C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

Pointer arithmetic in C


Pointer arithmetic in C allows you to perform arithmetic operations on pointer variables. When you perform arithmetic operations on a pointer, it advances or moves the pointer to point to another memory location based on the size of the data type the pointer is pointing to.

Increment and decrement operations in pointer

To understand the pointer arithmetic, let us consider the following array:

 

In pointer if a number is added to the pointer, then the pointer points to:

[address + number X sizeof(data type)] memory location. 

Basically the addition operation points to the next memory location whereas subtraction operation points to previous memory location as: 

[address - number X sizeof(data type)] memory location.

Now , let us understand the concept of pointer arithmetic using following C program:

#include <stdio.h>
int main() {
    int arr[] = {10, 20, 30, 40, 50, 60, 70, 80},*ptr;
    ptr = &arr[0];
    printf("value=%d\taddres%u\n", *ptr, ptr);
    ptr = ptr + 3;
    // points to next 3 position of current position: value 40
    printf("After adding 3 value=%d\taddres%u\n", *ptr, ptr); 
    ptr = ptr - 1;
     // points to previous 1 position of current position: value 30
     printf("after subtracting 1 value=%d\taddres%u\n", *ptr, ptr);
    return 0;
}

In pointer arithmetic, there are certain operations that are restricted or undefined due to the nature of pointers and memory access. Here are some of the operations that are restricted or not allowed in pointer arithmetic:

Multiplication and Division: Unlike addition and subtraction, multiplication and division operations are not defined for pointers. It doesn't make sense to multiply or divide a pointer by a scalar value.

int *ptr = ...;
ptr = ptr * 2; // Not allowed

Incrementing or Decrementing Pointers by Floating-Point Values: Pointer arithmetic doesn't support incrementing or decrementing pointers by floating-point values. Pointers are typically incremented or decremented by integral values, which represent offsets in memory.

int *ptr = ...;
ptr = ptr + 2.5; // Not allowed

Pointer-to-Pointer Arithmetic: Performing arithmetic operations between two pointers is not allowed. Pointers are not meant to be used for arithmetic operations with other pointers.

int *ptr1 = ...;
int *ptr2 = ...;
int *ptr3 = ptr1 + ptr2; // Not allowed

Pointer-to-Pointer Subtraction: Subtracting one pointer from another is only allowed if both pointers point to elements of the same array. This operation yields the number of elements between the two pointers, not the actual memory address difference.

int arr[5];
int *ptr1 = &arr[2];
int *ptr2 = &arr[4];
int diff = ptr2 - ptr1; // Allowed, yields 2

Pointer Comparison with Arithmetic: Pointer comparison with arithmetic operations (like less than, greater than, etc.) is not meaningful because pointers don't necessarily represent contiguous memory addresses.

int *ptr1 = ...;
int *ptr2 = ...;
if (ptr1 < ptr2) {...} // Allowed, compares memory addresses
if (ptr1 + 2 < ptr2) {...} // Not meaningful, comparing addresses with offset