A pointer is an address, means it is a numerical value. Hence, arithmetic operation can be done on a pointer. There are four operations that can be done on a pointer. Those are:
1. Increment (++) and Decrement (- -)
2. Addition of any integer to pointer (+)
3. Subtraction of any integer from pointer (-)
4. Subtracting two pointers of same datatype (-)
Don’t know what is Addition and Subtraction of a Pointer in C? Read: Addition and Subtraction of a Pointer (Pointer Arithmetics) in C
Here, we’ll discuss about Increment and Decrement of pointers.
When a pointer is incremented by 1, actually the pointer increments by the number equal to the size of the data type of the pointer. As an example if we increment a integer type pointer which holds the value of an address say 2000 then when it is incremented, the address becomes 2004 as the size of integer is 4 bytes in 64 bit machine.
Let’s see an example:
Example 1
/* C program to perform Increment of pointer */
/* www.codewindow.in */
#include <stdio.h>
// Driver code
int main() {
int a=5;
int *ptr; // Integer type pointer
ptr=&a; // Stores the address of 'a' variable
printf("Before increment address of the variable is: %u", ptr);
++ptr; // Incrementing the pointer by 1;
printf("\nAfter increment address of the variable is: %u", ptr);
return 0;
}
Output
Before increment address of the variable is: 612368028
After increment address of the variable is: 612368032
Explanation
In the above example after incrementing the ptr variable by one the ptr variable will incremented by 4 bytes as it is handling a integer type variable of size 4 bytes in a 64 bit machine. This increment operation moved the pointer to the next memory location without affecting the actual value which is 5. You can print and check it.
If the same operation has been done while ptr pointing to a character then after increment it will give current address+1 i.e 612368029 as size of char datatype is 1 byte.
Now let’s talk about Decrement too. It’s quiet similar like increment.
When a pointer is decremented by 1, actually the pointer decreases by the number equal to the size of the datatype of the pointer. As an example if we decrement a integer type pointer which holds the value of an address say 2000 then when it is decreased, the address becomes 1996 as the size of integer is 4 bytes in 64 bit machine.
An example will definitely solve your doubt
Example 2
/* C program to perform Decrement of pointer */
/* www.codewindow.in */
#include <stdio.h>
// Driver code
int main() {
int a=5;
int *ptr; // Integer type pointer
ptr=&a; // Stores the address of 'a' variable
printf("Before decrement address of the variable is: %u", ptr);
--ptr; // Decrementing the pointer by 1;
printf("\nAfter decrement address of the variable is: %u", ptr);
return 0;
}
Output
Before decrement address of the variable is: 164108828
After decrement address of the variable is: 164108824
Let us take another example of decrement of pointers using an array.
Example 3
/* C program to perform Decrement of pointer using array */
/* www.codewindow.in */
#include <stdio.h>
// Driver code
int main () {
int a[] = {10, 20, 30};
int i, *ptr;
ptr = &a[2]; // Let's have the address of a[2] in ptr pointer
for (i = 2; i >= 0; i--) {
printf("Address of a[%d] = %u", i, ptr ); // Print address of elements
printf("\nValue of a[%d] = %d\n", i, *ptr ); // Print value of that index
--ptr; // Pointer moves to the previous memory location
}
return 0;
}
Output
Address of a[2] = 3872619352
Value of a[2] = 30
Address of a[1] = 3872619348
Value of a[1] = 20
Address of a[0] = 3872619344
Value of a[0] = 10
Explanation
As the pointer is pointing to an array of integers hence the for decrementing the pointer every time the address will be decremented by 4 bytes, as per the size of an integer.
NOTE: The your output address may not match with us even with each execution it can change. But it is guaranteed that the difference between both addresses will be same with us (if you use 64 bit machine)
You Missed
- IBM Off-Campusing Drive | Application Developer: Java Full Stack | codewindow.in
- IBM Off-Campusing Drive | Brand Technical Specialist | codewindow.in
- Mindtree Off-Campusing Drive | Technical Architect – Adobe Analytics | codewindow.in
- Cognizant Off-Campusing Drive | GenC – Java Developer | codewindow.in
- Capgemini Off-Campusing Drive | Data Analyst | codewindow.in
- Accenture Off-Campusing Drive | Bilingual Hiring- Tech-Associate Software Engineering | codewindow.in
- Infosys Off-Campusing Drive | Associate Finance | codewindow.in
- Mindtree Off-Campusing Drive | GCP BigQuery – Technical Lead | codewindow.in
- Wipro Off-Campusing Drive | Project Engineer | codewindow.in
- Top 100 Java Interview Questions & Answers | Set 2 – codewindow.in
- TCS NQT Solved Paper 12th Sept 2021 – Part 6 – codewindow.in
- Morgan Stanley Off-Campusing Drive | Analyst | codewindow.in
- footer 2022
- Elementor #31191
- TCS NQT Solved Paper 12th Sept 2021 – Part 5 – codewindow.in
- Top 100 Java Interview Questions & Answers | Set 1 – codewindow.in
- Xl Dynamics Solutions Recruitment Drive – Multiple Roles – Codewindow.in
- TCS NQT Solved Paper 12th Sept 2021 – Part 4 – codewindow.in
- KPIT Technologies hiring 2022 Batch – Trainee Engineer – Codewindow.in
- Protected: Off Campus Recruitment Drive #31034
- TCS NQT Solved Paper 12th Sept 2021 – Part 3 – codewindow.in
- TCS NQT Solved Paper 12th Sept 2021 – Part 2 – codewindow.in
- TCS NQT Solved Paper 12th Sept 2021 – Part 1 – codewindow.in
- CDK Global Recruitment Drive – Graduate Trainee – Codewindow.in
- Microsoft Off Campus Recruitment Drive 2023 – SWE – Codewindow.in
- Aptitude Made Easy | Average – codewindow.in
- Accenture Non-Tech Recruitment Drive – B.com/BBA – Codewindow.com
- Aptitude Made Easy | Work and Time – codewindow.in
- Google Off Campus Hiring Drive – Software Engineer – Codewindow.in
- Aptitude Made Easy | Problems on Numbers – codewindow.in
- Symmetric Difference Code
- ZOHO Off-Campusing Drive | Software Developer | codewindow.in
Also Checkout
- Algorithm (6)
- Aptitude (10)
- Capgemini Coding Questions (2)
- Capgemini Pseudocode (4)
- CodeVita (3)
- Coding Questions (247)
- Cognizant Placement (11)
- Data Structure and Algorithm (69)
- Epam Full Question Paper (6)
- Guidance for Accenture (2)
- HR Questions (11)
- IBM Questions (5)
- Infosys (10)
- Internship (1)
- Interview Experience (27)
- Interview Questions (2)
- JECA (1)
- Job Info (416)
- Machine Learning (1)
- Miscellaneous (65)
- NPCI (2)
- Programming in C (58)
- Programming in C++ (6)
- Programming in JAVA (17)
- Programming in Python (132)
- Quiz (9)
- Recruiting Companies (36)
- Revature (3)
- Study Material (4)
- TCS NQT (70)
- Tech Mahindra Coding Questions (4)
- Tech Mahindra Questions (8)
- Uncategorized (65)
- Verbal Ability (8)
- Web Development (18)
- Wipro Coding Questions (5)
- Wipro NLTH (46)
- WIpro NLTH Coding Solve (31)