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!\n"); test_node_two_t txt; test_node_one_t tx; txt.c='c'; txt.to=&tx; sum(&txt); return 0; } main.c
posted on 2018-01-19 16:03 Bright Leopold 阅读(211) 评论(0) 编辑 收藏 举报