2) Prefix Increment/Decrement (++x, --x) : First changes the value, then uses it.
3) Postfix Increment/Decrement (x++, x--) – First uses the value, then changes it.
Operator | Description | Precedence | Associativity |
---|---|---|---|
Postfix ++ and -- | Postfix Increment and Decrement | 1 | Left to Right |
Prefix ++ and -- | Prefix Increment and Decrement | 2 | Right to Left |
Dereference (*) | Pointer Dereference | 2 | Right to Left |
The difference between prefix and postfix in combination with dereference is given below.
Expression | Meaning | Explanation |
---|---|---|
*ptr++ | Dereference pointer, then increment pointer. | *(ptr++) Uses ptr for dereference, then increments ptr. |
*++ptr | Increment pointer, then dereference. | *(++ptr) Increments ptr first, then dereferences the new value. |
(*ptr)++ | Dereference pointer, then increment value pointed by ptr. | Dereferences ptr first, then increments the value stored at ptr. |
++*ptr | Increment the value pointed to by the pointer, then use it. | No pointer moves only value is incremented. |
int *ptr, res;
(1) res = *ptr++;
(2) res = *++ptr;
(3) res = ++*ptr;
(4) res = (*ptr)++;
In the expression res = *ptr++;, there are two operators: the postfix increment (++) and the dereference (*) operator. As discussed earlier, the precedence of the postfix increment operator is higher than that of the dereference operator. Therefore, the expression *ptr++ is equivalent to *(ptr++). This means the increment operator is applied to ptr itself, not to the value pointed to by ptr. Since the increment operator is postfix, the current value of ptr is used in the expression first, and then ptr is incremented.
Thus, the expression res = *ptr++; is equivalent to writing:
res = *ptr;
ptr = ptr + 1;
#include<iostream>
using namespace std;
int main()
{ int arr[] = {11,22,33,44,55}; int *ptr,res; ptr = arr; cout<<"The ptr Points Initially : "<<ptr<<endl; res = *ptr++; cout<<"After Executing Expression"<<endl; cout<<"Value of res : "<<res<<endl; cout<<"The ptr Points : "<<ptr;
}
The ptr Points Initially : 0x6dfed4
After Executing Expression
Value of res : 11
The ptr Points : 0x6dfed8
In the expression res = *++ptr;, there are two operators: the prefix increment (++) and the dereference (*) operator. The prefix increment and dereference operators have the same precedence, and their associativity is from right to left. Therefore, the expression *++ptr is equivalent to *(++ptr). This means that first, ptr will be incremented, and then the new value of ptr will be used in the expression.
Thus, the expression res = *++ptr; is equivalent to writing:
ptr = ptr + 1;
res = *ptr;
#include<iostream>
using namespace std;
int main()
{ int arr[] = {11,22,33,44,55}; int *ptr,res; ptr = arr; cout<<"The ptr Points Initially : "<<ptr<<endl; res = *++ptr; cout<<"After Executing Expression"<<endl; cout<<"Value of res : "<<res<<endl; cout<<"The ptr Points : "<<ptr;
}
The ptr Points Initially : 0x6dfed4
After Executing Expression
Value of res : 22
The ptr Points : 0x6dfed8
The expression res = ++*ptr is equivalent to res = ++(*ptr). Here, the increment operator is applied to the value pointed to by ptr, not to the pointer ptr itself. Therefore, the pointer ptr does not change, but the value it points to is incremented.
#include<iostream>
using namespace std;
int main()
{ int arr[] = {11,22,33,44,55}; int *ptr,res; ptr = arr; cout<<"The ptr Points Initially : "<<ptr<<endl; res = ++*ptr; cout<<"After Executing Expression"<<endl; cout<<"Value of res : "<<res<<endl; cout<<"The ptr Points : "<<ptr;
}
The ptr Points Initially : 0x6dfed4
After Executing Expression
Value of res : 12
The ptr Points : 0x6dfed4
In the expression res = (*ptr)++; the increment operator is applied to *ptr. Since it is a postfix increment, the current value pointed to by ptr is first assigned to res, and then the value pointed to by ptr is incremented by 1.
Thus, the expression res = (*ptr)++; is equivalent to writing:
res = *ptr;
*ptr = *ptr + 1;
#include<iostream>
using namespace std;
int main()
{ int arr[] = {11,22,33,44,55}; int *ptr,res; ptr = arr; cout<<"The ptr Points Initially : "<<ptr<<endl; res = (*ptr)++; cout<<"After Executing Expression"<<endl; cout<<"Value of res : "<<res<<endl; cout<<"The ptr Points : "<<ptr<<endl; cout<<"The *ptr Value : "<<*ptr;
}
The ptr Points Initially : 0x6dfed4
After Executing Expression
Value of res : 11
The ptr Points : 0x6dfed4
The *ptr Value : 12
Let us now consider a widely discussed question from the BCI (Basic Computer Instructor) Exam 2022. Although this question was removed from the exam due to the presence of an incorrect option, it remains valuable for analysis and learning purposes. In this session, we will carefully examine the question and discuss the concepts involved to enhance understanding.
(a) *(ptrdata++)
(b) (*ptrdata)++
(c) *(ptrdata)++
(d) Depends on compiler
Option (a): *(ptrdata++) : Dereference first, then increment pointer. This correct interpretation. In this the pointer moves, value at original location accessed.
Option (b): (*ptrdata)++ : Here, the value stored at the pointer’s location is incremented, not the pointer.
Option (c): *(ptrdata)++ : Because of operator precedence, postfix ++ has higher precedence than *. So *(ptrdata)++ is exactly the same as *(ptrdata++) means this looks different but is parsed the same ways as (a). This is why the question was considered ambiguous and deleted.
Option (d): Depends on Compiler : This is not true in standard C++. The precedence rules are well-defined by the C++ standard, so the result is not compiler-dependent.
Below are the C++ code examples that clearly explain this concept. By running these programs, we can observe how the increment operator (++) and the dereference operator (*) behave when combined in different ways. Each option shows whether the increment is applied to the pointer itself or to the value stored at the pointer’s location.
This practical demonstration helps remove the confusion caused by operator precedence and associativity rules in C++. It also explains why options (a) *(ptrdata++) and (c) *(ptrdata)++ end up being identical, while (b) (*ptrdata)++ behaves differently by incrementing the stored value instead of the pointer.
#include <iostream>
using namespace std;
int main()
{ int arr[] = {10, 20, 30, 40, 50}; int *ptrdata; ptrdata = arr; *ptrdata++; //original expression given in question. cout<<"Using Original Expression *ptrdata++"<<endl; cout<<"Value : "<<*ptrdata<<endl; cout<<"Pointer Points : "<<ptrdata<<endl; cout << "Using Option (A) Expression *(ptrdata++)"<<endl; ptrdata = arr; // Reset Pointer. *(ptrdata++); cout << "Value: "<<*ptrdata<<endl; cout << "Pointer Points : " <<ptrdata<<endl; cout << "Using Option (B) Expression (*ptrdata)++"<<endl; ptrdata = arr; // Reset Pointer. (*ptrdata)++; cout << "Value: "<<*ptrdata<<endl; cout << "Pointer Points : " <<ptrdata<<endl; cout << "Using Option (C) Expression *(ptrdata)++"<<endl; ptrdata = arr; // Reset Pointer. *(ptrdata)++; cout << "Value: "<<*ptrdata<<endl; cout << "Pointer Points : " <<ptrdata<<endl; cout << "Using Option (D) Expression, Depends on Compiler"<<endl; cout << "Not applicable. All Behaviors are Standard in C++."<<endl; return 0;
}
Using Original Expression *ptrdata++
Value : 20
Pointer Points : 0x6dfedc
Using Option (A) Expression *(ptrdata++)
Value: 20
Pointer Points : 0x6dfedc
Using Option (B) Expression (*ptrdata)++
Value: 11
Pointer Points : 0x6dfed8
Using Option (C) Expression *(ptrdata)++
Value: 20
Pointer Points : 0x6dfedc
Using Option (D) Expression, Depends on Compiler
Not applicable. All Behaviors are Standard in C++.
I sincerely hope you enjoyed this session (blog). Our constant effort is to explore every concept thoroughly and explain it in depth so that the understanding becomes clearer and more meaningful. We are committed to helping you grasp the topics properly with detailed insights and easy-to-follow explanations. Thank you very much for your continuous support and encouragement. Your love and appreciation motivate us to keep improving and delivering quality content.
Thank you so much for taking the time to read my Computer MCQs Section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.