如何在头文件中引用 typedef? [英] How do I refer to a typedef in a header file?

查看:111
本文介绍了如何在头文件中引用 typedef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义了 typedef 结构的源文件:

I have a source file where a typedef struct is defined:

typedef struct node {
    char *key;
    char *value;
    struct node *next;
} *Node;

在这个模块中,有一些函数在 Node 上运行并以 Node 作为返回类型.我应该在这个 typedef 的头文件中写什么?

In this module, there are some functions that operate on a Node and have Node as return type. What am I supposed to write in the header file for this typedef?

只写对吗

typedef *Node;

在标题中?

推荐答案

你可以使用:

typedef struct node * Node;

但我建议不要在类型声明中隐藏指针.在变量声明中包含这些信息会提供更多信息.

But I would advise against hiding the pointer in type declaration. It is more informative to have that information in variable declaration.

module.c:

#include "module.h"
struct node {
    char *key;
    char *value;
    struct node *next;
};

module.h:

typedef struct node Node;

某处指针的变量声明:

#include "module.h"
Node * myNode; // We don't need to know the whole type when declaring pointer

这篇关于如何在头文件中引用 typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆