small questions related with pointers

               int x=48;
      int *p=&x;
      int n=(*p)++;
      cout << x << endl;

why the value of x is different ? in these two -I think it has got something to do with operator precedence - But why in the world would somebody need things like these ?

                int x=48;
      int *p=&x;
      int n=*p++;
      cout << x << endl;
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Yes, its because of operator precendece

HAve a look at this link.
It shows that the precednence of ++ is gretaer than deference [*].

Hence the result.

Also, the compiler has to atke care of every possile scenaroio, hence it is designed as per certain rules, one of them being the operator precendence

It is nothing to do with operator precedence.

p is a pointer, which you initialize to point to x.

"(*p)++" does two things. First it dereferences p, which yields a reference to x (since p is a pointer to x), then it increments the referenced item (x). Consequently, x has now been incremented.

This is the whole purpose of pointers --- you can directly update a variable without knowing which it is. e.g.

int x=3;
int y=99;

int* p=&x;
std::cout<<*p<<std::endl; // prints 3;
std::cout<<*p<<std::endl; // prints 99

(*p)++; // increment y
(*p)++; // increment x
anshumaEngineeringAuthor Commented:
and what about *p++
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Sorry, I missed the second code snippet in your question. I thought you were asking why the value of x changed.

*p++ is different to (*p)++ due to operator precedence as suggested.

*p++ is equivalent to *(p++), which returns the result of *p, then increments p. Consequently x is unchanged, but p is incremented in the second example, whereas x is incremented but p is unchanged in the first example.

Note that in both cases, n is the same, since post increment (something++) returns the value before the increment.
I will explain with one example

assume the addres  of x is 1000.

the value of x is 48.

int *p=&x    in this stmt you are assigning the address of x.

so for the statent   (*p)++  the value in the address of x will be increment by 1

but in the case of statement  *p++    the address of x will be added by 2 and it returns the value stored in the location 1002.

Note :
Since it is integer if you increment the adress by 1 it will automatically incremented by 2 because integer needs 2 bytes.

pls post ur comment for this answer
anshumaEngineeringAuthor Commented:

int x=48;
     int *p=&x;

       cout << &x << endl;
     cout << x << endl;

       int n=*p++;

       cout << &x << endl;
     cout << x << endl;

If the address of x was increased by 2 then x should have some garbage value in it. Nothing happens to the address of x by *p++ at least in visual C++ 6.0
The expression *p++ does not affect the value of x. It dereferences and increments p; it is equivalent to the two expressions *p and p++.

int n=*p++; //1

int n=*p; //2

//1 and //2 are both equivalent.

This is really only useful in the case of arrays,in which case it moves p to point to the next entry:

int someArray[10];

int *p=&someArray[0]; //get address of first element


int n=*p++;
cout<<n<<endl; //prints 3
cout<<p<<endl; // this prints the same as
cout<<&someArray[1]<<endl; // this
cout<<*p<<endl; // prints 5

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.