指针的输入输出模型是指,在主调函数中分配内存(输入模型)还是被调函数分配内存(输出模型)
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>
//指针做输出:被调用函数分配内存
//指针做输入:主调函数分配内存
//求文件中两段话的长度
int getMem(char**myp1,int *mylen1,char**myp2,int *mylen2)
{
char *temp1 = NULL;
char *temp2 = NULL;
temp1 = (char*)malloc(100);
if (temp1 == NULL)
{
return -1;
}
strcpy(temp1,"abcdfefg");
*mylen1 = strlen(temp1);
*myp1 = temp1;
temp2 = (char*)malloc(100);
if (temp2 == NULL)
{
return -1;
}
strcpy(temp2,"12345678");
*mylen2 = strlen(temp2);
*myp2 = temp2;
return 0;
}
int getMem_free(char**myp1)
{
char *p = NULL;
if (myp1 == NULL)
{
return -1;
}
p = *myp1;
free(p);
*myp1 = NULL;
return 0;
}
int main()
{
char*p1 = NULL;
int len1 = 0;
char*p2 = NULL;
int len2 = 0;
int ret = 0;
ret = getMem(&p1,&len1,&p2,&len2);
printf("p1:%s\n",p1);
printf("p2:%s\n",p2);
getMem_free(&p1);
getMem_free(&p2);
system("pause");
return 0;
}