edited by
1,555 views
2 votes
2 votes
int main()
{
    int m=44;
    int *p=&m;
    int &r=m;
    int n=(*p)++;
    int *q=p-1;
    r=--*(p)+1;
    ++*q;
    printf("m=%d n=%d r=%d",m,n,r);
    return 0;
}

 

Options:

  1. $m=44,\ n=46,\ r=45$
  2. $m=45,\ n=44,\ r=45$
  3. $m=46,\ n=44,\ r=46$
  4. $m=46,\ n=43,\ r=46$
edited by

1 Answer

2 votes
2 votes
int m=44 // 44 is assigned to m
int *p=&m // p now points to m and any change in *p will affect m
int &r=m //&r refers to m and any change in r will affect m
int n=(*p)++ // As it's a postfix operator first (*p)=44 will be assigned to n and then value of (*p=m=44+1=45)
int*q=p-1 //Stores the memory location just before &m and is none of our concern
r=--*(p)+1 //As it's prefix operator ,it first decrements (*p=45-1=m=44) and then adds 1 to it,so r=m=45
++*q// it's none of our concern as it just increments whatever was stored in *(p-1)

∴m=45,n=44,r=45 (b) is the answer

 

 

Related questions

1 votes
1 votes
1 answer
1
0 votes
0 votes
1 answer
2
0 votes
0 votes
2 answers
3