What is the difference between ++*p and (*p)++

Questions by vijayn

Showing Answers 1 - 3 of 3 Answers

sujatha

  • Sep 13th, 2007
 

int main ()
{
  int firstvalue = 5, secondvalue = 15;
  int * p1, * p2;

  p1 = &firstvalue;
  p2 = &secondvalue;

  cout << "firstvalue is " << firstvalue << endl;
  cout << "secondvalue is " << secondvalue << endl;

  cout << "++*p1 is " << ++*p1 << endl;
  cout << "(*p1)++ is " << (*p1)++ << endl;
 return 0;
}


output:

firstvalue is 5
secondvalue is 15
++*p1 is 6
(*p1)++ is 6

Ramakrishna Reddy

  • Oct 26th, 2007
 

hi!

++*p means prefix of the variable.

Ex1:
class
{
int  *p=4;
int  *q=++*p;
s.o.p(*q);
}

output:
the value of  *q is 5.

*p++ means postfix of the variable.
Ex2:

class
{
int *p=4;
int  *q=*p++;
s.o.p(*q);
}

output:
the value of *q is 4.

Ex3:

class
{
int i=5;
int j=i++;
int k=i++;
int t=i;
s.o.p(t);
}

output:
the value of j is 5
the value of k is 6
the value of t is 7.

  Was this answer useful?  Yes

harpreet sran

  • Nov 15th, 2007
 

in ++*p firstly it will move to the incremented address of the pointing variable &from there the value will pick up the value of the variable, then it will assign to the corresponding variable or to itself.  In (*p)++ it will first assign value and then will increment its pointing position. 

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions