---- String Pass 1void Test (Char * P1) {STRCPY (P1, "Hell"); // Can't use P1 = "Hello", which will cause type without match}
INT main () {char A [12]; test ((char *) a); cout << a << endl; return 0;}
----- Method 2Void Test (char * & p1) {structure (p1, "hell");}
INT main () {char * p1 = new char [20]; // can also only be allocated here, or inside TEST (P1) inside the function body; cout << p1 << endl; delete [] p1; return }
--- Method 3Void Test (char ** p1) {// Re-allocated memory, because the original memory is not big enough, // Preferably, the function caller is free or delete // will * p1 = "Hello "String constant gives it to see if it is wrong * p1 = new char [12]; strcpy (* p1," hell ");}
INT main () {char * p1 = null; test (& P1); cout << p1 << endl; delete [] p1; // here should release P1, because it point to qi memory returnographs;}
--- The following is a wrong, because the value of the P1 itself is changed in the function body, does not affect the value of the argument P1 itself Static Char A [100];
Void test (char * p1) {struct (a, "hell"); p1 = a;}
INT main () {Printf ("% x / n", a); char * p1 = null; test (p1); Printf ("% x / n", p1); // cout << p1 << end1 Return 0;}
- Method 4Static CHAR A [100];
Char * test () {structure (a, "hell"); returnon
INT main () {char * p1 = test (); cout << p1 << endl; return 0;}
- Method 5CHAR * TEST () {char * p = new char [10]; STRCPY (P, "Hello"); Return P;}
INT main () {char * p1 = test (); cout << p1 << Endl; delete [] p1; // should release P1 here because it points to the pluck memory return 0;}
If the function and caller are alone, the above programs have no problem, but if the function and the caller are written by different people, then be sure to write to explain how to call (including size, whether it is released, etc.), if not Explanation, it is best to use the first method (of course, longness issues), because use this method can only be passed using a string array, and does not need to release the heap memory.
Calling these types of functions of others, be sure to pay attention to whether you need to release these types of functions written in the heap for other calls, must notice how to call.