Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Functions and Pointers
  5. Pointer Arithmetic
  6. Pointer Subtraction

Pointer Subtraction

Pointer Subtraction

Example: Subtraction of two pointers

#include<stdio.h>
int main()
{
	int *ptr1 = (int *) 2000;
	int *ptr2 = (int *) 2008;
	int t;
	t = ptr2 - ptr1;
	printf("subtraction of two pointers = %d",t);
	return 0;
}

Output

subtraction of two pointers = 2

Example: Subtraction of integer from pointer

#include<stdio.h>
int main()
{
	int *ptr = (int *) 2000;
	printf("Initial value of ptr = %u\n",ptr);
	ptr = ptr -3;
	printf("Final value of ptr = %u",ptr);
	return 0;
}

Output

Initial value of ptr = 2000
Final value of ptr = 1994

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments