How to access a local variable from a different function using pointers? How to access a local variable from a different function using pointers? c c

How to access a local variable from a different function using pointers?


myArray is a local variable and as thus the pointer is only valid until the end of its scope (which is in this case the containing function getArray) is left. If you access it later you get undefined behavior.

In practice what happens is that the call to printf overwrites the part of the stack used by myArray and it then contains some other data.

To fix your code you need to either declare the array in a scope that lives long enough (the main function in your example) or allocate it on the heap. If you allocate it on the heap you need to free it either manually, or in C++ using RAII.

One alternative I missed (probably even the best one here, provided the array is not too big) is to wrap your array into a struct and thus make it a value type. Then returning it creates a copy which survives the function return. See tp1's answer for details on this.


You can't access a local variable once it goes out of scope. This is what it means to be a local variable.

When you are accessing the array in the replaceNumberAndPrint function the result is undefined. The fact it appears to work first time is just a fortunate coincidence. Probably the memory location you are pointing to is unallocated on the stack and is still correctly set for the first call, but the call to printf then overwrites this by pushing values onto the stack during its operation which is why the second call to printf displays something different.

You need to store the array data on the heap and pass a pointer, or in a variable that remains in scope (e.g. a global or something scoped within the main function).


Try something like that. The way you do it "kills" myArray cause if it locally defined.

#include <stdio.h>#include <stdlib.h>void replaceNumberAndPrint(int * array) { printf("%i\n", array[0]); printf("%i\n", array[1]); printf("%i\n" , array[2]); free(array);}int * getArray() { int * myArray = malloc(sizeof(int) * 3); myArray[0] = 4; myArray[1] = 64; myArray[2] = 23; //{4, 65, 23}; return myArray;}int main() { replaceNumberAndPrint(getArray());}

More : http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/

Edit: As Comments correctly pointed out: A better way to do it would be that :

#include <stdio.h>#include <stdlib.h>void replaceNumberAndPrint(int * array) {    if(!array)        return;    printf("%i\n", array[0]);    printf("%i\n", array[1]);    printf("%i\n" , array[2]);}int * createArray() {    int * myArray = malloc(sizeof(int) * 3);    if(!myArray)        return 0;    myArray[0] = 4;    myArray[1] = 64;    myArray[2] = 23;    return myArray;}int main() {    int * array = createArray();    if(array)    {        replaceNumberAndPrint(array);        free(array);    }    return 0;}