#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> /*间接赋值成立的三个条件: 条件一://定义一个实参 //定义一个形参 条件二://建立关联:把实参取地址传给形参 条件三://形参间接去修改实参的值 */ /* 用n级指针形参,去间接的修改n-1级指针(实参)的值。 */ /*间接赋值的场景 //1 2 3 这3个条件 写在有一个函数 //12 写在一块 3 单独写在另外一个函数里面 =====>函数调用 //1 23写在一块 void main() { char from[128]; char to[128] = {0}; char *p1 = from; char *p2 = to; strcpy(from, "1122233133332fafdsafas"); while (*p1 != '\0') { *p2 = *p1; p2 ++; p1++; } printf("to:%s \n", to); } */ 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); //1级指针 *myp1 = tmp1; //2级指针的间接赋值 tmp2 = (char*)malloc(200); strcpy(tmp2, "abcdefghigklmn"); *mylen2 = strlen(tmp2);//1级指针 *myp2 = tmp2;//2级指针的间接赋值 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; }