-2
#include<stdlib.h>
#include<iostream>

using namespace std;

void fun(int* a){
    int b=*a++;
    cout<<b<<endl;
}
void fun1(int *a){
    int b=*a+1;
    cout<<b<<endl;
}
int main(){
    int n=5;
    fun(&n);//output remains 5
    fun1(&n);//output incremented by 1
}

In the function fun the value of n does not get incremented when done as shown in the above code, on the other hand in function fun1 the value of n gets incremented by 1.What is the problem with the first approach to increment n?

1 Answers1

0

The behavior you observe is expected by the operator precendence rules.

In the first case it is a++, the previous value of a dereferenced.

In the second: the value of a dereferenced plus 1.

Yuki
  • 3,857
  • 5
  • 25
  • 43