BLOG DETAILS

Read the complete blog details covering in-depth explanations, practical insights, and examples. Designed for computer science learners and technology enthusiasts who want clarity, guidance, and knowledge in simple, structured form.

C++ Pointer Tricks : Understanding Dereference (*) Increment (++) and Decrement (--) Operators | Detailed & Practical Solution.

Understanding Pointer Expressions in C and C++

Pointer expressions that combine the dereference operator (*) with increment (++) or decrement (--) operators are among the most confusing topics for beginners in C and C++ programming. Expressions such as *ptr++, *++ptr, ++*ptr, and (*ptr)++ may look similar, but they produce completely different results because of operator precedence and associativity.

Understanding how these expressions are evaluated is essential for writing correct pointer-based programs. These concepts are also frequently asked in competitive examinations, university exams, and technical interviews because they test your understanding of pointers and operator evaluation.

Definition: Operator precedence determines which operator is evaluated first, while associativity decides the direction of evaluation when multiple operators have the same precedence.

Operators Used in Pointer Expressions

Before understanding complex pointer expressions, let's briefly review the operators involved.

OperatorPurposeDescription
*Dereference OperatorAccesses the value stored at the memory address pointed to by a pointer.
++xPrefix IncrementIncrements the value first and then uses the updated value.
x++Postfix IncrementUses the current value first and then increments it.
--xPrefix DecrementDecrements the value first and then uses the updated value.
x--Postfix DecrementUses the current value first and then decrements it.
Remember: The dereference operator (*) accesses the value stored at the memory location, whereas the increment and decrement operators may modify either the pointer or the value depending on how the expression is written.

Operator Precedence and Associativity

The result of a pointer expression depends on the precedence and associativity of the operators involved. Therefore, before solving any pointer expression, it is important to know which operator is evaluated first.

OperatorOperationPrecedenceAssociativity
x++, x--Postfix Increment / DecrementHighestLeft to Right
++x, --xPrefix Increment / DecrementNextRight to Left
*Dereference Operator
Exam Point: Postfix increment (x++) has higher precedence than the dereference operator (*). Therefore, expressions like *ptr++ are always interpreted as *(ptr++).

Comparison of Common Pointer Expressions

The following table summarizes the most commonly used pointer expressions and their meanings. Understanding these four expressions will eliminate most of the confusion related to pointers and increment operators.

ExpressionEquivalent FormMeaning
*ptr++*(ptr++)Use the current pointer to access the value, then move the pointer to the next memory location.
*++ptr*(++ptr)Move the pointer first, then access the value at the new location.
++*ptr++(*ptr)Increment the value pointed to by the pointer, then use the updated value.
(*ptr)++(*ptr)++Use the current value first and then increment the value stored at that memory location.

Sample Expressions Used Throughout This Article

To understand the behavior of different pointer expressions, we will use the following declarations in all upcoming examples.

int *ptr;
int result;

/* Pointer Expressions */

result = *ptr++;
result = *++ptr;
result = ++*ptr;
result = (*ptr)++;

In the next sections, we will analyze each expression individually. For every expression, we will understand:

  • How the compiler evaluates the expression.
  • Which operator is executed first.
  • Whether the pointer changes or the stored value changes.
  • A complete C++ program demonstrating the behavior.
  • The corresponding program output with detailed explanation.

Quick Revision

  • Operator precedence determines which operator is evaluated first.
  • Associativity determines the evaluation direction when operators have the same precedence.
  • Postfix increment has higher precedence than the dereference operator.
  • Prefix increment and dereference operators have the same precedence and are evaluated from right to left.
  • Always identify whether the pointer changes or the value changes before evaluating a pointer expression.

Expression 1: *ptr++

The expression *ptr++ is one of the most frequently asked pointer expressions in C and C++. At first glance, many students assume that the value pointed to by ptr is incremented. However, this interpretation is incorrect.

Since the postfix increment operator (++) has higher precedence than the dereference operator (*), the compiler evaluates the expression as:

*(ptr++)

This means the compiler first uses the current pointer to access the value and then moves the pointer to the next memory location. The value stored at the original memory location remains unchanged.

Equivalent Steps
result = *ptr;
ptr = ptr + 1;
Important: The pointer changes, but the value stored in memory does not.

Programming Example

#include <iostream>
using namespace std;
int main()
{
    int arr[] = {11,22,33,44,55};
    int *ptr;
    int result;
    ptr = arr;
    cout << "Pointer Initially : " << ptr << endl;
    result = *ptr++;
    cout << "After Executing Expression" << endl;
    cout << "Result = " << result << endl;
    cout << "Pointer = " << ptr << endl;
    return 0;
}

Output

Pointer Initially : 0x....
After Executing Expression
Result = 11
Pointer = Next Address
Explanation: The value 11 is assigned to result. After that, the pointer moves to the next array element (22). The value stored in the array remains unchanged.

Expression 2: *++ptr

The expression *++ptr looks very similar to *ptr++, but its behavior is completely different.

Here, the compiler evaluates the expression as:

*(++ptr)

The prefix increment operator increases the pointer before it is used. Therefore, the pointer first moves to the next memory location, and only then is the value at the new location accessed.

Equivalent Steps
ptr = ptr + 1;
result = *ptr;
Important: The pointer moves first, and then the new value is read.

Programming Example

#include <iostream>
using namespace std;
int main()
{
    int arr[] = {11,22,33,44,55};
    int *ptr;
    int result;
    ptr = arr;
    cout << "Pointer Initially : " << ptr << endl;
    result = *++ptr;
    cout << "After Executing Expression" << endl;
    cout << "Result = " << result << endl;
    cout << "Pointer = " << ptr << endl;
    return 0;
}

Output

Pointer Initially : 0x....
After Executing Expression
Result = 22
Pointer = Next Address
Explanation: The pointer moves to the second element of the array before dereferencing. Therefore, result stores 22 instead of 11.

Comparison: *ptr++ vs *++ptr

ExpressionPointer MovementReturned ValueEquivalent Form
*ptr++After dereferencingCurrent value*(ptr++)
*++ptrBefore dereferencingNext value*(++ptr)

Quick Revision

  • *ptr++ ? Use the current pointer, then move it.
  • *++ptr ? Move the pointer first, then use it.
  • Both expressions modify the pointer.
  • Neither expression changes the value stored in memory.
  • Always remember that postfix increment has higher precedence than the dereference operator.

Expression 3: ++*ptr

Unlike the previous two expressions, ++*ptr does not move the pointer. Instead, it increments the value stored at the memory location pointed to by ptr.

The compiler interprets this expression as:

++(*ptr)

Here, the dereference operator accesses the value first, and then the prefix increment operator increases that value before it is used. The pointer continues to point to the same memory location throughout the operation.

Equivalent Steps
*ptr = *ptr + 1;
result = *ptr;
Important: The value stored in memory changes, but the pointer address remains unchanged.

Programming Example

#include <iostream>
using namespace std;
int main()
{
    int value = 10;
    int *ptr = &value;
    int result;
    result = ++*ptr;
    cout <<"Result = " << result << endl;
    cout <<"Value = " << value << endl;
    cout <<"Pointer = " << ptr << endl;
    return 0;
}

Output

Result = 11
Value = 11
Pointer = Same Address
Explanation: The value stored at the memory location is increased from 10 to 11. Since prefix increment is used, the updated value is assigned to result. The pointer itself never moves.

Expression 4: (*ptr)++

This expression also modifies the value stored at the memory location, but unlike the previous example, it uses the postfix increment operator.

The current value is used first, and then the value stored in memory is increased by one. The pointer remains unchanged throughout the operation.

(*ptr)++
Equivalent Steps
result = *ptr;
*ptr = *ptr + 1;
Important: The old value is returned first, and only after that is the stored value incremented.

Programming Example

#include <iostream>
using namespace std;
int main()
{
    int value = 10;
    int *ptr = &value;
    int result;
    result = (*ptr)++;
    cout <<"Result = " << result << endl;
    cout <<"Value = " << value << endl;
    cout <<"Pointer = " << ptr << endl;
    return 0;
}

Output

Result = 10
Value = 11
Pointer = Same Address
Explanation: The current value (10) is assigned to result. After that, the value stored in memory becomes 11. The pointer still points to the same memory location.

Comparison of All Four Pointer Expressions

ExpressionPointer Changes?Value Changes?Description
*ptr++YesNoUse the current value, then move the pointer.
*++ptrYesNoMove the pointer first, then access the next value.
++*ptrNoYesIncrement the stored value first, then use it.
(*ptr)++NoYesUse the current value first, then increment it.
Easy Trick to Remember
  • If ++ is attached to the pointer, the pointer moves.
  • If ++ is attached to the dereferenced value, the stored value changes.
  • Prefix performs the operation first.
  • Postfix uses the current value first and performs the operation afterward.

Quick Revision

  • ++*ptr increments the value before using it.
  • (*ptr)++ uses the current value before incrementing it.
  • Neither expression changes the pointer address.
  • Both expressions modify the value stored at the memory location.
  • Always determine whether the increment operator is applied to the pointer or to the dereferenced value before evaluating the expression.

Previous Year Examination Question

Consider the following C statement:

result = *ptr++;

Which of the following correctly describes the execution of this statement?

  1. The value pointed to by ptr is incremented.
  2. The pointer is incremented before accessing the value.
  3. The current value is accessed first, then the pointer is incremented.
  4. Both pointer and value are incremented.
Correct Answer: Option (C)

Step-by-Step Compiler Evaluation

Whenever you encounter a pointer expression in an examination, do not try to guess the answer. Instead, follow a systematic approach.

  1. Identify all operators present in the expression.
  2. Check their precedence using the operator precedence table.
  3. Apply associativity if operators have the same precedence.
  4. Rewrite the expression using parentheses.
  5. Determine whether the pointer changes or the stored value changes.
  6. Finally, calculate the output.

Let us now examine a widely discussed question from the BCI (Basic Computer Instructor) Exam 2022. Although this question was later removed because it had two equivalent correct options, making it ambiguous, it is still an excellent example for understanding operator precedence and associativity in C++. In this section, we will analyze the question carefully and explain the reasoning behind each option.

BCI 2022 Previous Year Question

Question : The 'PTRDATA' is a pointer to a data type. The expression *PTRDATA++ is evaluated as:
(A) *(ptrdata++)
(B) (*ptrdata)++
(C) *(ptrdata)++
(D) Depends on Compiler

Explanation of Each Option

Let's examine each option individually to understand why this question was considered ambiguous.

Option (A): *(ptrdata++)
This interpretation is correct. Since the postfix increment operator (++) has higher precedence than the dereference operator (*), the pointer is incremented after its current value is used. In other words, the value at the current memory location is accessed first, and then the pointer moves to the next memory location.

Option (B): (*ptrdata)++
This expression behaves differently. Here, the increment operator is applied to the value stored at the memory location pointed to by ptrdata, not to the pointer itself. As a result, the pointer remains unchanged while the stored value is incremented.

Option (C): *(ptrdata)++
According to C++ operator precedence rules, this expression is interpreted exactly the same as *(ptrdata++). Since the postfix increment operator has higher precedence than the dereference operator, both Option (A) and Option (C) represent the same expression. This is the primary reason why the question was considered ambiguous and was later removed from the examination.

Option (D): Depends on Compiler
This option is incorrect. In standard C++, operator precedence and associativity are clearly defined by the language specification. Therefore, the evaluation of this expression does not depend on the compiler.

Important Note: The ambiguity in this question arises because both Option (A) and Option (C) are interpreted identically by the C++ compiler according to the language's operator precedence rules.

The following C++ programs demonstrate the behavior of each expression. By running these examples, you can observe how the increment operator (++) and the dereference operator (*) interact when combined in different ways. Each example clearly shows whether the increment operation affects the pointer itself or the value stored at the memory location.

These practical demonstrations help eliminate the confusion surrounding operator precedence and associativity. They also explain why Options (A) and (C) produce identical behavior, while Option (B) 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 the question
    cout << "Using Original Expression *ptrdata++" << endl;
    cout << "Value : " << *ptrdata << endl;
    cout << "Pointer Points : " << ptrdata << endl;
    // Option (A)
    cout << "\nUsing Option (A) Expression *(ptrdata++)" << endl;
    ptrdata = arr; // Reset Pointer
    *(ptrdata++);
    cout << "Value : " << *ptrdata << endl;
    cout << "Pointer Points : " << ptrdata << endl;
    // Option (B)
    cout << "\nUsing Option (B) Expression (*ptrdata)++" << endl;
    ptrdata = arr; // Reset Pointer
    (*ptrdata)++;
    cout << "Value : " << *ptrdata << endl;
    cout << "Pointer Points : " << ptrdata << endl;
    // Option (C)
    cout << "\nUsing Option (C) Expression *(ptrdata)++" << endl;
    ptrdata = arr; // Reset Pointer
    *(ptrdata)++;
    cout << "Value : " << *ptrdata << endl;
    cout << "Pointer Points : " << ptrdata << endl;
    // Option (D)
    cout << "\nUsing Option (D) Expression, Depends on Compiler" << endl;
    cout << "Not applicable. All behaviors are standard in C++." << endl;
    return 0;
}

Common Mistakes Made by Students

  • Assuming that *ptr++ increments the value instead of the pointer.
  • Ignoring operator precedence while solving expressions.
  • Confusing prefix and postfix increment operators.
  • Believing that all four pointer expressions produce the same output.
  • Not distinguishing between changing the pointer and changing the value stored in memory.
  • Skipping parentheses while mentally evaluating expressions.

Common Interview Questions

  1. Explain the difference between *ptr++ and *++ptr.
  2. What is the difference between ++*ptr and (*ptr)++?
  3. Which pointer expressions modify the pointer?
  4. Which pointer expressions modify the stored value?
  5. Why is operator precedence important in pointer expressions?
  6. How does postfix increment differ from prefix increment?
  7. How would you evaluate a complex pointer expression during an interview?

Important Exam Points

  • Postfix increment has higher precedence than the dereference operator.
  • *ptr++ is interpreted as *(ptr++).
  • *++ptr is interpreted as *(++ptr).
  • ++*ptr increments the stored value before using it.
  • (*ptr)++ uses the current value first and then increments it.
  • Always determine whether the increment operator applies to the pointer or the dereferenced value.

Conclusion

Pointer expressions are an important part of C and C++ programming because they combine pointers with increment and decrement operators. Although expressions such as *ptr++, *++ptr, ++*ptr, and (*ptr)++ appear similar, they perform completely different operations due to operator precedence and associativity.

Instead of memorizing these expressions, understand how the compiler evaluates them step by step. Once you know whether the increment operator applies to the pointer or the dereferenced value, solving such questions becomes much easier.

Questions based on pointer expressions are frequently asked in UGC NET, SET, PGT, TGT, Computer Instructor, DSSSB, university examinations, and technical interviews. Regular practice with different examples will improve both your confidence and problem-solving speed.

Final Revision

  • *ptr++ ? Access the current value, then move the pointer.
  • *++ptr ? Move the pointer first, then access the next value.
  • ++*ptr ? Increment the stored value first, then use it.
  • (*ptr)++ ? Use the current value first, then increment it.
  • Operator precedence determines which operation happens first.
  • Associativity determines the evaluation direction when operators have the same precedence.
  • Always rewrite complex expressions using parentheses before evaluating them.
Keep Practicing!

Pointer expressions become easy to understand with regular practice. Try tracing each expression on paper, draw simple memory diagrams, and predict the output before running the program. This habit will strengthen your understanding of pointers and help you solve competitive exam and interview questions accurately.

Suresh Kulahry

About the Author

Suresh Kulahry

Suresh Kulahry is the Founder of Suraku Academy and an educator dedicated to helping students prepare for Computer Science examinations through simple explanations, high-quality study material, MCQs, Previous Year Questions, and practical tutorials. His mission is to make computer science learning easy, accessible, and free for every learner.

🎉 Thank You for Reading!

We hope this article helped you understand the topic better. Continue your Computer Science preparation with more free resources available on Suraku Academy.