zoukankan      html  css  js  c++  java
  • c语言函数参数类似继承的传递

    函数的参数如果是一个父结构的指针,

    这个结构包含在另一个子结构中,

    typedef struct test_node_one test_node_one_t;
    typedef struct test_node_two test_node_two_t;
    
    struct test_node_one{
            int t1;
    };
    
    struct test_node_two{
        test_node_one_t *to;
         char c;
    };

    然后定义调用函数

    #include "t1.h"
    
    void sum(test_node_one_t *t1){
        test_node_two_t      *n;
        n=(test_node_two_t*)t1;
        printf("%c",n->c);
    }

    调用

        test_node_two_t txt;
        test_node_one_t tx;
        txt.c='c';
        txt.to=&tx;
        sum(&txt);

    输出:c

    完整代码

    #ifndef T1_H_INCLUDED
    #define T1_H_INCLUDED
    
    
    typedef struct test_node_one test_node_one_t;
    typedef struct test_node_two test_node_two_t;
    
    struct test_node_one{
            int t1;
    };
    
    struct test_node_two{
        test_node_one_t *to;
         char c;
    };
    
    void sum(test_node_one_t *t1);
    
    
    #endif // T1_H_INCLUDED
    
    t1.h
    
    
    #include "t1.h"
    
    void sum(test_node_one_t *t1){
        test_node_two_t      *n;
        n=(test_node_two_t*)t1;
        printf("%c",n->c);
    }
    
    t1.c
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "t1.h"
    
    int main()
    {
        printf("Hello world!
    ");
        test_node_two_t txt;
        test_node_one_t tx;
        txt.c='c';
        txt.to=&tx;
        sum(&txt);
        return 0;
    }
    
    
    main.c
  • 相关阅读:
    了解Boost神器
    官方教程避坑:编译ARM NN/Tensorflow Lite
    信号量 PV 操作
    MAC 读写 ntfs 格式的硬盘
    poj题目分类
    Gelfond 的恒等式
    那些scp里的烂梗
    b
    jmeter集合
    Jmeter文章索引贴
  • 原文地址:https://www.cnblogs.com/weichao975/p/8317445.html
Copyright © 2011-2022 走看看