- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Given an example of C pointer addition and subtraction
Pointers have many but easy concepts and they are very important to C programming.
Two of the arithmetic pointer concepts are explained below, which are C pointer addition and subtraction respectively.
C pointer addition
C pointer addition refers to adding a value to the pointer variable.
The formula is as follows −
new_address= current_address + (number * size_of(data type))
Example
Following is the C program for C pointer addition −
#include<stdio.h> int main(){ int num=500; int *ptr;//pointer to int ptr=#//stores the address of number variable printf("add of ptr is %u
",ptr); ptr=ptr+7; //adding 7 to pointer variable printf("after adding add of ptr is %u
",ptr); return 0; }
Output
When the above program is executed, it produces the following result −
add of ptr is 6422036 after adding add of ptr is 6422064
C pointer subtraction
It subtracts a value from the pointer variable. Subtracting any number from a pointer variable will give an address.
The formula is as follows −
new_address= current_address - (number * size_of(data type))
Example
Following is the C program for C pointer subtraction −
#include<stdio.h> int main(){ int num=500; int *ptr;//pointer to int ptr=#//stores the address of number variable printf("addr of ptr is %u
",ptr); ptr=ptr-5; //subtract 5 to pointer variable printf("after sub Addr of ptr is %u
",ptr); return 0; }
Output
When the above program is executed, it produces the following result −
addr of ptr is 6422036 after sub Addr of ptr is 6422016
- Related Articles
- Explain addition and subtraction of fractions with example.
- Explain Closure property of addition and subtraction with example.
- Addition and Subtraction of Matrix using pthreads in C/C++
- Explain addition and subtraction of algebraic equations.
- Signals and Systems: Addition and Subtraction of Signals
- Explain the addition and subtraction of integers with examples.
- Give properties of addition and subtraction for whole numbers.
- C++ program to find addition and subtraction using function call by address
- Complete the addition-subtraction box.(a)(b)"
- How MySQL performs date arithmetic with addition and subtraction operators?
- Explain the performance of Addition and Subtraction with signed magnitude data in computer architecture?
- How Addition Of Positive Number Is Similar To Subtraction Of Negative Number
- Which of the following statements is false.(a) Addition is commutative for integers(b) Subtraction is commutative for integers(c) Integers are closed under addition condition(d) Integers are closed under subtraction condition
- Explain the concept of Array of Pointer and Pointer to Pointer in C programming
- Explain the concept of pointer to pointer and void pointer in C language?

Advertisements