指针的输入输出特性

//指针做函数参数具有输入输出的特性//被调函数分配内存叫做输出,主调函数分配内存叫做输入

#define  _CRT_SECURE_NO_WARNINGS 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int  getMem_My(char **myp1/*out*/, int *mylen1 /*out*/, char **myp2 /*out*/, int *mylen2 /*out*/)

{
char *p1 = NULL;
p1 = (char *)malloc(100);
strcpy(p1,"abcd123455");
//间接赋值 
*myp1 = p1; //2级指针的间接赋值
int tmplen1 = strlen(p1);

*mylen1 = tmplen1; //1级指针

char *p2 = NULL;
p2 = (char *)malloc(100);
strcpy(p2, "12345abcd");
*myp2 = p2;
int tmplen2 = strlen(p2);
*mylen2 = tmplen2; //1级指针
return 0;
}

int main()
{
int ret = 0;
char **myp1 = NULL;
int len1 = 0;
char **myp2 = NULL;
int len2 = 0;
ret = getMem_My(&myp1, &len1, &myp2, &len2);
if (ret != 0)
{
ret = -1;
printf("getMem_My():err %d\n",len1);
}
printf("p1:%s %d\n", myp1,len1);
printf("p2:%s %d\n", myp2, len2);

if (myp1 != NULL)
{
free(myp1);
myp1 = NULL;
}
if (myp2 != NULL)
{
free(myp2);
myp2 = NULL;
}

system("pause");
return ret;
}

猜你喜欢

转载自blog.csdn.net/a6taotao/article/details/80084913