-2

Why we can return the value of a local variable from a function but not it's address? Such as we can write 'return a' but not 'return &a' in C.

  • Simply because local variables go out of scope when the function is executed. – Kishor Oct 15 '17 at 05:17
  • 3
    Possible duplicate of [What and where are the stack and heap?](https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – rkosegi Oct 15 '17 at 05:17
  • #include int* f() { /*int a = 6, *p; p = &a; printf("Address of a = %p or %p\n", &a, p); return p; */ int a = 6; return &a; } int main() { int x=5, *y; y = f(); printf("\nvalue at address %p of y = %d\n", y, *y); return 0; } See this program. Here we can return the address by pointer p which is also a local variable then how is it possible? – user2047585 Oct 15 '17 at 05:22
  • 2
    Possible duplicate of [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – Ilja Everilä Oct 15 '17 at 05:26
  • And to your comment (which should be a part of the question): https://stackoverflow.com/questions/8743411/return-address-of-local-variable-in-c – Ilja Everilä Oct 15 '17 at 05:28
  • [6.2.4p2: "If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime."](http://port70.net/~nsz/c/c11/n1570.html#6.2.4p2) – Ilja Everilä Oct 15 '17 at 05:42
  • @TomKarzes Concering "You can return the address of a local variable" , even that is UB. – chux - Reinstate Monica Oct 15 '17 at 05:44

2 Answers2

1

A modified version of your example in the comments:

#include <stdio.h>

int* f() {
   int a = 6, *p;
   p = &a;
   return p;
}  

int g() {
    int a = 6;
    return a;
}

int main() {
   int x = g();

   int* y = f();
}

Both f and g return by value.

One returns an int and stores the value in x. Now x holds the value 6 previously in a.

The other function returns a pointer and stores the pointer value in y. Now y holds the value previously stored in p.

The problem is just that the pointer is now invalid (dangling), as the thing p pointed to no longer exists.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
-1

Actually, you can.

But you have to define your function correctly like this:

int a = 0;

int* ReturnValAddress()
{
    return &a;
}

Although you can send any local variable address, pay attention please that sending the function's local variable address does not have any meaning because right after returning, local variables would die!!, but you can send global variable address like above code.

  • 3
    That's not a local variable. Move the definition of `a` into the function, and prefix it with `static`, and then it will be. – Peter Oct 15 '17 at 05:32
  • I didn't say 'a' is a local variable, I said that generally returning any local variable is wrong, please read it again carefully. – Hadi Robati Oct 15 '17 at 05:46
  • Read the QUESTION carefully. It was about local variables, so you haven't addressed the question as asked. – Peter Oct 15 '17 at 05:59