Re: Return Array from C++
Quote originally posted by tlee ...
However, my current knowledge does not allow me to see how the pointer can solve this C++ limitation (return a pointer might be dangerous because it might point to the address of local variable, and this variable will go out of scope. Hence, this pointer will point to invalid value). I appreciate for your information.
C++ Syntax (Toggle Plain Text)
#include int *foo(int *array) { int *start = array; while ( *array ) { std::cout << *array++ << ' '; } std::cout << class="me2">endl; return start; } int *bar(int *array) { int *start = array; while ( *array ) { *array++ += 1; } return start; } int main() { int myarray[] = {1,2,3,4,5,0}; foo(bar(foo(bar(foo(myarray))))); return 0; } /* my output 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 */

没有评论:
发表评论