c - typedef 在同一结构上使用两次

标签 c struct

我试图理解一段代码中的结构用法。这让我很困惑。看起来 typedef 在同一个结构上使用了两次。请任何人都可以帮助我理解为什么这段代码有两次 typedef 。有什么办法可以简化这段代码吗? 非常感谢您抽出时间。

typedef struct {
city_t* cities; 
int count;      
cost_t cost;    
} tour_struct;
typedef tour_struct* tour_t;


typedef struct {
tour_t* list;
int list_sz;
int list_alloc;
}  stack_struct;
typedef stack_struct* my_stack_t;

最佳答案

第一个 typedef 为(匿名)结构提供类型名。第二个 typedef 定义了指向另一个的指针类型。

tour_struct tour; // declares a struct.
tour_t ptr; // declares a pointer to a struct.

关于c - typedef 在同一结构上使用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27091127/

相关文章:

c - OpenMP 循环并行化

c - 解码汇编回调

c - 如何将全局变量传递给函数以及如何计算数组的输入?

c - 在 C 中从另一个图窗访问指向图窗的指针

结构布局 : can't make a constructor to set the field I need

c - 程序在编译快要结束时崩溃

c# - 我应该使用静态类还是结构以外的其他东西?

c - 如何从 tcp 头 bin 文件读取/写入

c - 为什么未释放的内存会导致泄漏?以及其他一些问题

c++ - C++中带有模板变量的结构