Pointer Input and Output Model

Source: Internet
Author: User

Pointer Input and Output Model

 

# Define _ CRT_SECURE_NO_WARNINGS # include <stdio. h> # include <stdlib. h> # include <string. h> // pointer for output, memory allocated by the called function √ // pointer for input, memory allocated by the main function // calculate the length of int getNum (char ** myp1, int * mylen1, char ** myp2, int * mylen2) {int ret = 0; char * tmp1 = NULL; char * tmp2 = NULL; tmp1 = (char *) malloc (100); if (tmp1 = NULL) {return-1;} tmp1 = strcpy (tmp1, "abcdefg"); * mylen1 = strlen (tmp1 ); * myp1 = tmp1; // indirectly modify the value of the real parameter p1 tmp2 = (char *) ma Lloc (100); if (tmp2 = NULL) {return-2;} tmp2 = strcpy (tmp2, "11222"); * mylen2 = strlen (tmp2 ); * myp2 = tmp2; // indirectly modify the return ret value of the real parameter p2;} int getNum_Free (char ** myp1) {// if (myp1 = NULL) // {// return; //} // free (* myp1); // release the pointer variable, indicating the memory space. // * myp1 = NULL; // change the real parameter to NULL // or enter char * tmp; tmp = myp1; if (myp1 = NULL) {return-1;} tmp = * myp1; free (tmp); * myp1 = NULL; return 0;} int main () {char * p1 = NULL, * p2 = NULL; int len1 = 0, len2 = 0; int ret = 0; ret = getNum (& p1, & len1, & p2, & len2 ); if (! Ret) {printf ("p1: % s \ n", p1); printf ("p2: % s \ n", p2); getNum_Free (& p1 ); getNum_Free (& p2) ;}else {printf ("func getNum (): % d", ret) ;}system ("pause"); return 0 ;}

 

Related Article

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.