Good pointer --- use the function that returns the pointer value (continued)

Source: Internet
Author: User

For example, the function that returns the pointer value can also be interpreted as passing the memory using the pointer.

In addition to dynamic memory, a pointer can also be returned to a static storage area. However, the swap memory cannot be passed using pointers. Because the swap memory function disappears at the end, some junk content will be returned, which is invalid.

For the basic content, see: Good At Pointers-use functions that return pointer values

// When returning a pointer, in addition to the dynamic memory, you can also return the static storage area # include <iostream> # include <string> using namespace STD; // use the pointer to pass the value char * testpointerforwardvalue (char * pchar2) {char * pchar = new char [strlen (pchar2) + 1]; // use strlen () the function calculates the number of characters in the string. 1if (null = pchar) {exit (0);} strcpy (pchar, pchar2); cout <pchar <Endl; return pchar;} // use the pointer reference to pass the value char * testreferencepointer (char * & rpchar) {char * pchar = new char [strlen (rpchar) + 1]; if (null = pchar) {exit (0) ;} Strcpy (pchar, rpchar); cout <pchar <Endl; return pchar;} // use the returned pointer to pass the value: Pass the dynamic memory, if the swap memory is passed, an error occurs: char * testreturndynamicmemory (char a []) {char B [100]; char * pconst = "Sichuan University! "; Char * pchar = new char [strlen (A) + strlen (pconst) + 1]; If (null = pchar) {exit (0);} strcpy (B, a); // The cout <B <Endl; strcpy (pchar, a); strcat (pchar, pconst); Return pchar; // if it is changed to return B, the callback memory is returned, and the output content is invalid.} // use the returned pointer to pass the value: pass the static storage space char * testrenturstaticmeory () {char * P = "Diaoyu Islands is Chinese inherent territory. "; cout <p <Endl; // return P of the string that outputs the static memory space; // string that allows passing the static memory space} int main () {char A1 [] = "Hello, sichua N University! "; Char * P1 = A1; char A2 [] =" Hello, College of computer! "; Char * P2 = a2; char * P = NULL; cout <" sizeof (A1): "<sizeof (A1) <", sizeof (P1 ): "<sizeof (P1) <", strlen (P1): "<strlen (P1) <Endl; P = testpointerforwardvalue (P1 ); cout <p <Endl; Delete [] P; // The caller releases new memory space P = testreferencepointer (P2); cout <p <Endl; delete [] P; // The caller releases the new memory space P = testreturndynamicmemory (A2); cout <p <Endl; Delete [] P; // The caller releases the new memory space P = testrenturstaticmeory (); cout <p <Endl; System ("pause"); Return 0 ;}

Good pointer --- use the function that returns the pointer value (continued)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.