2,582 views
0 votes
0 votes
//gcc 5.4.0

#include<stdio.h>
int  main()
{
int i=3;
    float f=3.50,*ptr;
    float *multy(int ,float);
    ptr=multy(i,f);
   printf("\n%u %f",ptr,*ptr);
return 0;
}
float * multy(int ii,float jj)
{
float p=ii*jj;
    printf("%u  %f",&p,p);
return (&p);
}

2 Answers

Best answer
3 votes
3 votes
#include<stdio.h>
int  main()
{
    int i=3;
    float f=3.50,*ptr;
    float *multy(int ,float);
    ptr=multy(i,f);                          // Address Return
    printf("\n%u %f",ptr,*ptr);              // Printing
    return 0;
}

float * multy(int ii,float jj)
{
    float p=ii*jj;                           // Memory Allocation
    printf("%u  %f",&p,p);
    return (&p);
}

The function stack would look as under:

"Memory Allocation" happens in function multy(..). This memory is named as 'p'. The print inside the multy(..) works fine and prints the address and value of p as 'p' exists in that stack.

When address is returned from multy(..), the control returns to main function. The address returned is stored in 'ptr'. The memory which was allocated inside the stack of multy(..) is not valid to be used outside the scope of multy(..)

So, when "Printing" happens in main(..), the address is illegal (not meant to be used outside scope of multi(..) ), so you get invalid memory access / segmentation fault.

selected by
0 votes
0 votes
#include<stdio.h>
int  main()
{
    int i=3;                  1 memory created for integer variable i and 3 is assigned to it.
    float f=3.50, *ptr;       2 memory created for float variable f and 3.50 is assigned to it.
    float *multy(int ,float); 3 prototype of function multy
    ptr=multy(i,f);     4 call for function multy and multy function return to float pointer ptr. 
    printf("\n%u %f",ptr,*ptr);  9. here error may come or may not come since when multy 
function is over its all local variable are free i.e. not pointed by p but memory is 
pointed by ptr (may be it happen that location assigned to some other variable), gives 
error some time, if memory not assigned to anyone then no error.
return 0; 
}
float * multy(int ii,float jj) 5 call comes here and assigned 3 to int ii and f to  float jj.
{
float p=ii*jj;             6 memory created locally for float variable p and 3.0 *3.05 here 
not ii have type casted internaly not explicitly.
    printf("%u  %f",&p,p); 7. here address of p and value of p is printed.
return (&p);               8. return address of local variable p to ptr of main().
}

Related questions

1 votes
1 votes
1 answer
1
. asked Apr 2, 2017
1,584 views
What will be the output of the following C program? If you think it will give a runtime error, you need to mention it. In either case,your answer must include proper just...
0 votes
0 votes
1 answer
2
radha gogia asked Aug 10, 2015
398 views
CASE A: CASE A: &#8234;#&lrm;include&#8236;<stdio.h int divide( int a, b) { return 7; } int main() { int a=divide(8,3); printf("%d",a); return 0; }CASE B :CASE B : #inclu...
7 votes
7 votes
3 answers
3
Parshu gate asked Nov 20, 2017
773 views
What is the output of the following program?#include<stdio.h int main() { int array[]={10, 20, 30, 40}; printf(“%d”, -2[array]); return 0; }$-60$$-30$$60$Garbage valu...
1 votes
1 votes
1 answer
4