local-variables

returning a local variable from function in C [duplicate]

假装没事ソ 提交于 2019-11-25 22:19:29
问题 This question already has answers here : How to access a local variable from a different function using pointers? (9 answers) Closed 2 years ago . #include <stdio.h> int foo1(void) { int p; p = 99; return p; } char *foo2(void) { char buffer[] = \"test_123\"; return buffer; } int *foo3(void) { int t[3] = {1,2,3}; return t; } int main(void) { int *p; char *s; printf(\"foo1: %d\\n\", foo1()); printf(\"foo2: %s\\n\", foo2()); printf(\"foo3: %d, %d, %d\\n\", p[0], p[1], p[2]); return 0; } When I

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

痞子三分冷 提交于 2019-11-25 22:12:53
问题 May I have any access to a local variable in a different function? If so, how? void replaceNumberAndPrint(int array[3]) { printf(\"%i\\n\", array[1]); printf(\"%i\\n\", array[1]); } int * getArray() { int myArray[3] = {4, 65, 23}; return myArray; } int main() { replaceNumberAndPrint(getArray()); } The output of the piece of code above: 65 4202656 What am I doing wrong? What does the \"4202656\" mean? Do I have to copy the whole array in the replaceNumberAndPrint() function to be able to