C Programming :: Pointers - Discussion
48 / 60
#include<stdio.h>
int main()
{
int *ptr1,*ptr2;
ptr1=(int *)malloc(sizeof(int));
ptr2=func(20,10,ptr1);
printf("%d %d\n",*ptr1,*ptr2);
}
int *func(int a, int b, int *c)
{
int x=a+b;
*c=a-b;
return(&x);
}
#include<stdio.h>
int main()
{
int *ptr1,*ptr2;
ptr1=(int *)malloc(sizeof(int));
ptr2=func(20,10,ptr1);
printf("%d %d\n",*ptr1,*ptr2);
}
int *func(int a, int b, int *c)
{
int x=a+b;
*c=a-b;
return(&x);
}
ABug in the code
BNo Bugs prints correctly
CError
DNone of the above
Show Explanation
i.e after the execution of the func, the space allocated to it into the stack will be freed up after its completion where func is trying to send the address of the variable x which is declared inside the func and after dellocating its stack space there will no x be present at the location which is being returned by the func.
hence there is bug in the code.
Asked In ::
there is bug in the code :
i.e after the execution of the func, the space allocated to it into the stack will be freed up after its completion where func is trying to send the address of the variable x which is declared inside the func and after dellocating its stack space there will no x be present at the location which is being returned by the func.
hence there is bug in the code.
Read Full Answer
Report Error
Please Login First Click Here