I was writing a code related to referencing in C/C++. I made a pointer and put it into a function that incremented it. In the function, I wrote *ptr++
and tried to increment the value of the integer the pointer was pointing at.
#include <iostream>
using namespace std;
void increment(int *ptr)
{
int &ref = *ptr;
*ptr++; // gets ignored?
ref++;
}
int main()
{
int a = 5;
increment(&a);
cout << a;
return 0;
}
Can anyone please tell me why I can't increment the variable? I have tried incrementing it using +=1
, and it works but not by using ++
operator?