指针做函数参数的输入输出特性

输入特性:

  • 在主调函数中分配内存,被调函数使用

输出特性:

  • 被调函数中分配内存,主调函数使用

代码示例:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//1、输入特性, 主调函数中分配内存,被调函数中使用内存
void func(char*p)
{
   
        strcpy(p,"hello world");
}
void test01()
{
   
        //分配到栈上
        char buf[1024] = {
   0};
        func(buf);
        printf("%s\n",buf);
}
void printString(char *str)
{
   
        printf("%s\n",str);
}
void test02()
{
   
        //分配到堆区
        char*p = malloc(sizeof(char)*64);
        memset(p,0,64);
        strcpy(p, "hello world");
        printString(p);
}
//输出特性:被调函数分配内存,主要调用函数使用内存
void allocateSpace(char**pp)
{
   
        char *temp = malloc(sizeof(char)*64);
        memset(temp, 0, 64);
        strcpy(temp, "hello world");
        *pp = temp;
}
void test03()
{
   
        char*p = NULL;
        allocateSpace(&p);
        printf("%s\n",p);
}
int main()
{
   
        //test01();
        //test02();
        test03();
        return EXIT_SUCCESS;
}

更多文章,敬请关注微信公众号:YQ编程