Indirect pointer assignment, pointer assignment

Source: Internet
Author: User

Indirect pointer assignment, pointer assignment

# Define _ CRT_SECURE_NO_WARNINGS # include <stdio. h> # include <stdlib. h> # include <string. h>/* three conditions for indirect value assignment: Condition 1: // define a real parameter // define a form parameter. Condition 2: // establish an association: pass the real parameter fetch address to Condition 3: // The parameter indirectly modifies the value of the real parameter * // use the n-level pointer parameter to indirectly modify the n-level pointer (real parameter). * // * Indirect assignment scenario // 1 2 3 these three conditions are written in one function // 12 in one piece 3 separately written in another function ====> Function call // 1 23 and write it in a piece of void main () {char from [128]; char to [128] = {0}; char * p1 = from; char * p2 = to; strcpy (from, "1122233133332 fafdsafas "); while (* p1! = '\ 0') {* p2 = * p1; p2 ++; p1 ++;} printf ("to: % s \ n", );} */int getMem3 (char ** myp1, int * mylen1, char ** myp2, int * mylen2) {int ret = 0; char * tmp1, * tmp2; tmp1 = (char *) malloc (100); strcpy (tmp1, "1234456789"); * mylen1 = strlen (tmp1); // Level 1 pointer * myp1 = tmp1; // indirect value of level 2 pointer tmp2 = (char *) malloc (200); strcpy (tmp2, "abcdefghigklmn"); * mylen2 = strlen (tmp2 ); // Level 1 pointer * myp2 = tmp2; // indirect value assignment of level 2 pointer return ret;} int main () {char * p1 = NULL; char * p2 = NULL; int len1 = 0; int len2 = 0; int ret = getMem3 (& p1, & len1, & p2, & len2 ); if (ret! = 0) {printf ("func getMem3 % d", ret); return ret;} printf ("p1: % s \ n", p1); printf ("p2: % s \ n ", p2); if (p1! = NULL) {free (p1); p1 = NULL;} if (p2! = NULL) {free (p2); p2 = NULL;} 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.