间接赋值成立的条件
- 条件1 定义1个变量(实参) 定义1个变量(形参)
- 条件2 建立关联,把实参取地址传给形参
- 条件3 *形参去间接的修改了实参的值
间接赋值推论
在函数调用的时候
- 用1级指针形参,去间接修改了0级指针(实参)的值
- 用2级指针形参,去间接修改了1级指针(实参)的值
- 用3级指针形参,去间接修改了2级指针(实参)的值
- 用n级指针形参,去间接修改了n-1级指针(实参)的值
代码
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>int getMemPt(char **myp1, int *mylen1, char **myp2, int *mylen2) {int ret = 0;char *tmp1, *tmp2;tmp1 = (char *)malloc(100);strcpy(tmp1, "abcde");*mylen1 = strlen(tmp1); // 1级指针*myp1 = tmp1; // 2级指针tmp2 = (char *)malloc(200);strcpy(tmp2, "123456987");*mylen2 = strlen(tmp2); // 1级指针*myp2 = tmp2; // 2级指针return ret;}void main(){int ret = 0;char *p1 = NULL;int len1 = 0;char *p2 = NULL;int len2 = 0;ret = getMemPt(&p1, &len1, &p2, &len2);if (ret != 0) {printf("func getMemPt() err: %d\n", 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;}printf("p1: %d \t p2: %d\n", p1, p2);/*输出结果:p1:abcdep2:123456987p1: 0 p2: 0*/system("pause");return;}
