C subtracting integer value from pointer



Suppose we have subtracted “n” from pointer of any data type having initial addess as “init_address” then after subtraction we can write –

ptr = initial_address - n * (sizeof(data_type))

Subtracting integer value with Pointer

int *ptr , n;
ptr = &n ;
ptr = ptr - 3;

Live Example 1 : Decrement Integer Pointer

#include<stdio.h>

int main(){

int *ptr=(int *)1000;

ptr=ptr-3;
printf("New Value of ptr : %u",ptr);

return 0;
}

Output :

New Value of ptr : 994

Formula :

ptr = ptr   - 3 * (sizeof(integer))
    = 1000  - 3 * (2)
    = 1000  - 6
    = 994

Summary :

Pointer - Pointer = Integer
Pointer - Integer = Pointer