in Programming in C
832 views
2 votes
2 votes
int main()
{
    int *r2;
    void abc(int **);
    abc(&r2);
    printf("%d",*r2);
}
void abc(int **r3)
{
    int r1=5;
    /* add statements here */
}

Which statement is added to the above program such that the address of r1 gets stored in r2?

A) *r2 = &r1;

B)*r1 = &r3;

C) *r3 = &r1;

D) None of these

in Programming in C
832 views

4 Comments

Answer C) here

 but

void abc(int **);

this line inside main() is not needed

1
1
but , r1 is local variable to function abc. So after completion of function abc, *r3 will behave like dangling pointer.

correct me if i am wrong.
1
1
yes, even r2 will work like a dangling pointer.rt?
1
1
yes.
0
0

1 Answer

5 votes
5 votes
Best answer
int main()
{
    int *r2;
    void abc(int **);
    abc(&r2);
    printf("%d",*r2);
}
void abc(int **r3)
{
    Static int r1=5;
    *r3 = &r1;
}

Program should be like this, if address of r1 gets stored in r2 .

selected by
by

3 Comments

And if it is int r1 =5;  then for this particular question , we can go with ans D right ?

0
0
Then, if option undefined behaviour is here, i will go with that, as it is UB (segmentation fault)

But, nothing is here like that , Then, option D is best .
0
0
On executing the program with *r3=&r1, the output obtained is 5.
0
0

Related questions