#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int getContentLen(const char *filename, char **buf, int *len)
{
char *tmp = NULL;
tmp = (char *)malloc(100*sizeof(char));
if (buf == NULL)
{
*len = 10; //第一次调用求长度
}
else
{
strncpy(tmp, "demo12345678", 10);
*buf = tmp;
*len = 10;
}
return 0;
}
int main(int argc, const char * argv [])
{
const char *filename = "./demo.txt";
char *p = NULL;
int len = 0;
//第一次调用求长度
getContentLen(filename, NULL, &len);
p = (char *)malloc(len);
if (p == NULL)
{
return -1;
}
//第二次调用求内容
getContentLen(filename, &p, &len);
printf("content = %s\n", p);
if (p != NULL)
{
free(p);
p = NULL;
}
return 0;
}
C下使用指针作为函数参数完成两次不同操作
最新推荐文章于 2023-04-15 00:19:36 发布