Pointer parameter, level two pointer change value

Source: Internet
Author: User

Program:

#include <stdio.h>#include<stdlib.h>voidChangeChar*a) {//Copyprintf"Change 1:%s\n", a); A=" Change";//The argument copy points to a change without affecting the point of the argumentprintf"Change 2:%s\n", a);}voidChange2 (Char**a) {printf ("Change2 1:%s\n", *a); *a="Changea";//A is unchanged, changes a to the contents of the memory, a pointing to the memory is pointer A, changing the pointer a point, the original pointer A to the content is unchangedprintf"Change2 2:%s\n", *a);}voidChange3 (Char*a) {printf ("Change3 1:%s\n", a); //Char *b= (char *) malloc (sizeof (char) *10);//malloc allocates heap memory (but B itself is in the stack.) Stack allocation: char b[10], function return is released), unless the program exits without free//a=b;sscanf"Changeb","%s", a);//directly change the value of a point to addressprintf"Change3 2:%s\n", a); //Free (b);}voidChange4 (int*a) {//Copyprintf"change4 1:%d\n", *a); intb= the; A=&b;//with a copy of the argument, the copy points to a change, but does not affect the point of the original argument. Unless you change the value that the original argument points to, that is Change5printf"change4 2:%d\n", *a);}voidChange5 (int*a) {printf ("change5 1:%d\n", *a); *a= the;//A does not change the content of a pointing to memoryprintf"change5 2:%d\n", *a);}voidChange6 (int**a) {    intb= -; printf ("Change6 1:%d\n", **a); *a=&b;//the same as Change2 .printf"Change6 2:%d\n", **a);}intMain () {//    Char*a ="Main"; Char*a1=A; Change (a);//Pass in a first-level pointer, which is a copy of the pointer within the functionprintf"Main 1:%s\n\n", a);//no change, here is mainChange2 (&AMP;A);//Pass in a level two pointerprintf"Main 2:%s\n", a);//change to the value given in Change2printf"Main 2 a1:%s\n\n", A1);//Change2 just points a back to the new value, and the value pointed to by A1 is constant.    Charb[Ten] ="mainb";//cannot be here char *b, and the function does not allocate memory, otherwise sscanf error segment Spit Core//char *b= "XX";Change3 (b); printf ("Main 3:%s\n\n", b);//change the value given to sscanf in Change3    intC= -; Change4 (&c); printf ("Main 4:%d\n\n", c);//no change, or a.Change5 (&c); printf ("Main 5:%d\n\n", c);//change the new value given to Change5    int*d=&c;//the same as Change2 .    int*d1=D; Change6 (&AMP;D);//Pass in a level two pointerprintf"Main 6:%d\n", *d);//Changeprintf"Main 5:%d\n\n", *D1);//Original C value    return 0;}

Results:

Pointer parameter, level two pointer change value

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.