1

Why don't I get 20 as output in second printf statement?

int main() {
    int *p;
    int * fun();
    p = fun();
    printf("%d\n", p);
    printf("%d", *p);

    return 0;
}

int * fun() {
    int i = 20;
    return (&i);
}
red0ct
  • 4,840
  • 3
  • 17
  • 44
adi_tdkr
  • 127
  • 3
  • 9
  • 5
    Because `i` has gone out of scope. Possible [duplicate](http://stackoverflow.com/questions/4824342/returning-a-local-variable-from-function-in-c) – Weather Vane Jun 27 '16 at 13:35

3 Answers3

3

When fun exits, the local variable i ceases to exist, and any pointer to it is now invalid. Obviously, the memory cell that i used to occupy still exists, but it is now available for something else to use and may be overwritten between the time fun returns and you attempt to print *p.

Either have the function return the value of i:

int fun( void ) { int i; ...; return i; }

or write to a parameter

void fun( int *i ) { ...; *i = 20; ... }

If i had been declared static:

int *fun( void ) { static int i; i = 20; return &i; }

then this would work as you expect, since static objects exist over the lifetime of the program. However, it's not a good solution for this particular problem.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

You can't allocate an integer on stack (inside function scope in your case) and use it outside the scope.

The reason for this is that the memory allocated for your function (= the stack) is considered free after the function ends.

To dig a bit dipper - your process has virtual memory provided by the operating system. This memory is managed by the process and each function you call gets a disposable memory allocated to it by the process (this is the stack). Once the function returns, that memory is recycled by the application for other uses

so in your example:
call int *fun() <- all variables you declare and use, including alloca in this function exist in the function stack
return &1 <- now the function ended and the memory allocated for it is vacant again

This is the reason using that memory outside the function is illegal (thanks @Eugene) when trying to access that memory (integer i in your case)

Ishay Peled
  • 2,783
  • 1
  • 23
  • 37
-1

The variable i in the function is its local variable that is in general destroyed after exiting the function. So the returned pointer is invalid and the program has undefined behavior.

You can change the function the following way

int * fun( void )
{
    static int i = 20;
    return &i;
}

In this case the variable i will have static storage duration and will be alive after exiting the function.

Take into account that in C the following declarations

int * fun();

and

int * fun( void );

are not equivalent.

The first declaration means that there is nothing known about the function parameters. You should declare your function like

int * fun( void );

Also main without parameters should be declared in C like

int main( void )

And to output a pointer you should use format specifier %p instead of %d as you do

printf("%p\n",p);
        ^^^
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Giving the workaround for the stated problem by using a not-that-easy concept of static variables to someone not familiar with a concept of a scope is not a good idea. IMO. – Eugene Sh. Jun 27 '16 at 13:47
  • @EugeneSh. This your comment also does not make sense. I showed how the pointer can be made valid and nothing more. – Vlad from Moscow Jun 27 '16 at 13:48