in Programming in C edited by
1,612 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$
in Programming in C edited by
1.6k views

4 Comments

@srestha mam it should be syntax error

0
0

The above code should through an error due to this int &r = m statement because &r is not a variable.

https://stackoverflow.com/questions/27052775/what-is-the-difference-between-int-r-x-and-r-y-in-c

This is not the reason for the error.

0
0
edited by
Here the doubt is ...

Int &r=m; is valid or not.

See this statement creates  an alias for m;

Both r and m contents are same; moreover  we say both are pointing to same location.

If r change that is also reflect to m and vice versa.However this is a valid or not its look ambiguous in c.
0
0

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