-2
#include <iostream>
using namespace std;

void test(int x, int *y) {

*y = 5;

}

int main() {
int *a ,b =2 ;
a = &b;

test(*a, a);
a--; // a++ also give different value

This is the part i very confuse, I know if i dont put the (a--) statement the output will be 5. but what is really behind the meaning of a-- / a++ cause sometimes it give me different value as I test it with different value. I found this accidentally .

cout<<"d"<<*a<<endl;

}
lewisck
  • 1
  • 1

1 Answers1

1

a contains memory pointer to the variable. So it is *a = 5, if you increment/decrement a, then it will point to some other address/location in the memory which has some garbage value. That's what you are getting.

Lets say a -> [2000] //memory address which contains value 5 if you do a++/a-- then a will point to [2004]/[1996] location in the memory which has some garbage value.

Meghraj
  • 11
  • 2