c语言--二级指针在函数间的传递和使用

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

typedef struct{
    int index;
    char **my_data;
}Test_Stru;

typedef struct{
    int index;
    Test_Stru **test;
}Father_Stru;

void dump_test_stru(Test_Stru *test)
{
    if(!test){
        printf(" dump structure is null \n ");
        return;
    }

    printf("-------------->\n");
    printf("test index:= %d \n", test->index);
    if(*test->my_data){
        printf("test value:= %s \n ", *test->my_data);   
    }
    printf("<--------------\n");

    return;
}

void dump_test_stru_father(Father_Stru *f)
{
    if(!f){
        printf("your stru is null \n");
    }
    printf("------->\n");
    printf("f->index:= %d \n", f->index);
    if(*f->test){
        dump_test_stru(*f->test);
    }
    printf("<-------\n");
}

Test_Stru *test_second_rank_pointer()
{

    Test_Stru *test = (Test_Stru *)malloc(sizeof(Test_Stru));
    memset(test, 0, sizeof(Test_Stru));
    test->index=1;
    test->my_data=(char **)malloc(sizeof(char **));
    *test->my_data = strdup("hello world");
    return test;
}



Father_Stru *test_struct_call(Test_Stru *test)
{
    Father_Stru *father;

    father = (Father_Stru *)malloc(sizeof(Father_Stru));
    memset(father, 0, sizeof(Father_Stru));
    father->index=0;

#if 0
        //c的世界是个很有意思的世界。
        //当我们在被调函数中创建一个指针来接收参数,
        //它实际上可以看成原指针的一个副本,它的性质和原指针一样,是一个指针,而且和原指针指向同一个内存区域。
        //因为是被调函数中创建的,所以它的生命周期和所在函数相同,当函数返回时,也就被清空了。
        //所以,如果我们想保存数据给我们新创建的全局变量时,要把指针的值赋给新的变量,
        //而不能把把指针的地址赋给变量,因为这个指针只是一个副本,一旦函数返回,它存储的信息就会被释放。
    father->test =(Test_Stru **) malloc(sizeof(Test_Stru **));
    father->test=&test;
#else
    father->test =(Test_Stru **) malloc(sizeof(Test_Stru **));
    *father->test=test;
#endif
    
    if(*father->test){
        printf("This is my struct call, test->index:= %d \n", (*father->test)->index);
        printf("This is my struct call, value is: %s \n ", *(*father->test)->my_data );
    }

    return father;
}

Father_Stru *test_struct_call2(Test_Stru **test)
{
    Father_Stru *father;

    father = (Father_Stru *)malloc(sizeof(Father_Stru));
    memset(father, 0, sizeof(Father_Stru));
    father->index=0;
    father->test=test;
    
    return father;
}

int main(int argc, char* argv[])
{
    printf("prepare test fo make father stru------------------> \n");
    Test_Stru *test=test_second_rank_pointer();
    dump_test_stru(test);
    printf("<---------------------\n\n");

    printf("f1 stru make ====================>\n");
    Father_Stru *f1 = test_struct_call(test);
    dump_test_stru_father(f1);
    printf("<=================================\n\n");
    
    printf("f2 stru make ====================>\n");
    Father_Stru *f2 = test_struct_call2(&test);
    dump_test_stru_father(f2);
    printf("<=================================\n\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值