Hi guys!!

I am wondering how below given program is working. in function func1 how return (void *)y working.

#include<stdio.h>
void * func1()
{
    int y;
    y = 10;
    return (void *)y;
}
int main()
{
    void *x = 0x00;
    x = func1();
    if(x == NULL)
    {
      printf("error\n");
    }
    else
    {
      printf("value of x (%d)\n",x);
    }
    return 0;
}

i was expecting error print in this case.
thanks

It's not recommended to just program it like that because you return a pointer to a local (automatic) variable, and when the function returns, the variable will be 'destroyed' (this means: it doesn't exist anymore, the memory assigned to it will be marked: "allowed to use"), but you still have a pointer to that freed memory, it will be never guaranteed that the freed memory will still contain the value of variable x (10 in this case).
If a local variable goes out-of-scope, then the memory it was occupying will be freed, meaning that the memory may be used to store other things in it, so the value 10 may be overwritten during the program's execution.

That's just the spirit of C: It's not because it seems working that it's actually working.

How to overcome this?
Use dynamic memory allocation.

in fun1,when the program runs at " int y;",it will allocate a size of sizeof(int) bytes' space for the variable “y” in the stack area。

when your program return from fun1 ,it returns the address of local variable “y”
x = func1();

so now the x points to the previous “y”(but you should know that y is NOT exist now!)。Its value is y’s address,so x == NULL returns false。your progam won't print any error message。

return (void*) y; What do you do here?
You create a pointer and put the value of y in it, this means that you let the pointer point to memory address 10 (not to the value 10 !!) in your example, you don't return a pointer to the value.
Further in your program you think that you've a pointer to that value, but in fact, the value is just stored as a memory address in your pointer, which explains why your program gives the expected output.
For example if you'd put value 0 in variable y, your program would have displayed 'error', which directly explains why your code is wrong.
This can be very dangerous, because you think the pointer points to the memory location which holds the value, but after dereferencing it, you can easily get a segmentation fault.
(And this is only about reading the data, can you imagine what would happen if you change the data like this: *x = 35 ? No? I can: you put the value 35 on memory address 10, this is certainly not what you want)

Note:: My previous post only applies if you'd have written the return statement like this: return &y; (and in that case your compiler should probably warn you as well)

Thanks guys for your valuable comments!!!

Thanks Guys for your fantastic comments!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.