Variable survival problem

xiaoxiao2021-03-17  178

#include using namespace std; char * getm (void) {char p [] = "9cbs"; returnit p;} void test (void) {char * str = null; str = getm (); cout << Str << Endl;} int Main (void) {test (); return 0;} The program is not: 9cbs, but garbled, because P is an automatic variable in GETM, after the endm end, P The survival period is over, returns a pointer to the P variable that points to this survival period, which depends on whether the content of the original P object is contained.

When the function is called, the type of parameters is also important. The line ginseng is a value copy. It is easy to have problems when returning. The pointer will not have such problems, and how to change, the pointer points is constant.

转载请注明原文地址:https://www.9cbs.com/read-129486.html

New Post(0)