[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

C Programming :: Pointers - Discussion

Home > C Programming > Pointers > MCQs Questions Discussion

48 / 60

What is the output of the following 'C' program ?

#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

Answer: Option (Login/Signup)

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 :: Global Edge

Post Your Answer Here:     

Reply    
Rate This: +0 -0
    Report


Report Error

Please Login First Click Here