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
- Реальные Отзывы Людей О Выплатам И Игре В Адмирал Казино Онлайн
- Подпишитесь На 150 Бонусов Казино В Riobet Онлайн – Казино!
- Wipro NLTH Coding Solve | Digital Secure Data Transfer Solutions | Codewindow.in
- Elementor #57063
- 1xBet ile Etkili Bahis Stratejileri
- Ontdek Casino Online Buitenland: Voordelen en Meer
- Casino Online Buitenland Ervaar de Beste Spellen
- Азино Мобайл Зеркало – Преимущества И Недостатки Работы Онлайн
- Казино Польша Онлайн Бесплатные Игры
- Топ-5 Отечественных Систем Для Управления Проектами: Что Подходит Для Десятков Проектов Сервисы На Vc Ru
- Online Casino Buitenland Verkennen
- Online Casino Zonder Cruks: De Vrijheid van Spelen
- Лучшие Торговые Стратегии Форекс 2025: Обзор Самых Прибыльных
- vhod-v-kazino-r7
- 1King casino Türkiye Bahis Adresi — Güvenilir ve Hızlı
- onlayn-kasino-turbo
- oficialnyy-sayt-kazino-turbo
- pinko-kazino
- oficialnyy-sayt-kazino-r7
- pinko-oficialnyy-sayt
- pinko-zerkalo
- riobet-kazino
- riobet-kazino
- riobet-oficialnyy-sayt-kazino
- Casino zonder Cruks Ervaar Vrijheid en Spanning
- The Future of Science: How Emerging Technologies Are Transforming Research
- Winorio online casino – een recente ontdekking bedoeld voor Nederlandse spelers: pluspunten van deze recente internet gokhuis
- Bahis Dunyasinda Most bet ile Yerinizi Alin
- Natural Remedies for Potency: Effective Ways to Enhance Male Vitality
- рейтинги казино
- Mostbet ile bahis yaparak kazan
- Unieke Casino Ervaringen Zonder CRUKS Nederland
Also Checkout
- 3484 (1)
- Adobe (1)
- Advanced Coading (1)
- Advanced course (1)
- Ajax (17)
- Algorithm (6)
- Amagi (1)
- Amazon Interview Questions (1)
- Angular JS (23)
- anonymous (4)
- Aptitude (10)
- Aptitude tricks (3)
- Automata Fixing (1)
- Basic Coding (1)
- big data (61)
- Bookkeeping (1)
- Books (1)
- Bridge2i (1)
- C programming (20)
- Campgemini Interview Questions (1)
- Capgemini Coding Questions (2)
- Capgemini Pseudocode (4)
- casino online buitenland2 (2)
- casino zonder cruks 4 (1)
- casino zonder cruks nederland (1)
- Celebal Tech (1)
- Cloud Computing (28)
- code nation (2)
- Coding Questions (241)
- Cognizant Placement (11)
- commvault Systems (1)
- Computer Network (24)
- CSS (44)
- CTS (1)
- Data Science (44)
- Data Structure (1)
- Data Structure and Algorithm (126)
- DBMS (29)
- De Show Interview Questions (1)
- deloitte (2)
- Deutsche Bank Interview questions (1)
- ð¥ð¢ð╗ð░ð©╠åð¢ ð║ð░ðÀð©ð¢ð¥ ð┐ð¥ð╗ÐîÐêð░ (1)
- Enhance Communication (1)
- Epam Full Question Paper (6)
- Extempore (1)
- Exxon Mobil interview questions (1)
- filpkart (1)
- Fractal Analytics Interview Questions (1)
- general (1)
- Genpact (1)
- Grab (1)
- GreyB Interview Questions (1)
- Group Discussion (1)
- Guidance for Accenture (3)
- Gupshup (1)
- Hackathon 2024 (1)
- HCL Interview Questions (1)
- Hexaware (1)
- HFCL (1)
- HR Questions (11)
- HTML5 (44)
- httpscasinozonder-cruks.xyz (1)
- IBM Interview questions (1)
- IBM Questions (5)
- Incture Interview Questions (1)
- Infineon Technologies Interview Questions (1)
- Infosys (11)
- Infosys Interview Questions (1)
- Internship (1)
- Interview Experience (19)
- Interview Questions (63)
- IT Образование (1)
- ITC Infotech (1)
- itron (1)
- JavaScript (67)
- JECA (1)
- Job Info (93)
- JQuery (15)
- Kantar Interview Questions (1)
- Language Confusion (1)
- language confussion (1)
- Larsen & Turbo (1)
- Latenview AnalyticsInterview questions (1)
- Lexmark International Interview Questions (1)
- Linkedin (1)
- Machine Learning (23)
- Media.net (1)
- Mindtree Interview Questions (1)
- misc (2)
- Miscellaneous (61)
- Mock Test Series (2)
- MongoDB (34)
- Morgan Stanly Interview Questions (1)
- nagarro (5)
- navi (1)
- NodeJS (24)
- NTT Data Interview Questions (1)
- NVDIA (1)
- NVDIA interview questions (1)
- online casino buitenland (1)
- Operating System (19)
- Optum (1)
- other (2)
- PayU (2)
- Persistent INterview Questions (1)
- PHP and MYSQL (31)
- Previous Coding Questions (1)
- Programming in C (61)
- Programming in C++ (33)
- Programming in JAVA (154)
- Programming in Python (133)
- Pseudo Code (2)
- pseudocode (5)
- Public (1)
- PWC Interview Questions (1)
- Python (61)
- Quiz (9)
- Razorpay (1)
- ReactJS (26)
- Recruiting Companies (34)
- Revature (3)
- salesforce (1)
- Samsung (1)
- Schlumberger (1)
- Seimens (2)
- Slice (1)
- Smart Cube (1)
- Software development (1)
- Software Engineering (35)
- Study Material (4)
- Tally Solutions (1)
- tata cliq (1)
- TCS (1)
- TCS NQT (69)
- TCS NQT Coding Questions (13)
- Tech Mahindra Coding Questions (4)
- Tech Mahindra Questions (8)
- Technical Preparation (1)
- Teg Analytics (1)
- Tejas Network Interview Questions (1)
- Texas Instrument Interview Questions (1)
- Tiger Analytics (1)
- Uncategorized (86)
- UnDosTres (1)
- Unstop (1)
- Verbal Ability (8)
- Verbal Lesson (1)
- Web Development (208)
- wipro (1)
- Wipro Coding Questions (5)
- Wipro interview Questions (1)
- Wipro NLTH (31)
- WIpro NLTH Coding Solve (20)
- Zenser (1)
- Zoho Interview Questions (1)
- Форекс обучение (1)