具有未声明/未定义类型的 typedef 结构

typedef struct with undeclared/undefined type

本文关键字:typedef 结构 类型 未定义 未声明      更新时间:2023-10-16

我正在阅读有关VAD的webrtc源代码,我对代码感到困惑

typedef struct WebRtcVadInst VadInst;

我已经搜索了所有关于WebRtcVadInst的代码,没有找到任何与结构WebRtcVadInst相关的源代码。另一方面,我确实发现了一些关于VadInst的东西。

typedef struct VadInstT_ {
int vad;
int32_t downsampling_filter_states[4];
...
...
int init_flag;
} VadInstT;

VadInst* WebRtcVad_Create() {
VadInstT* self = (VadInstT*)malloc(sizeof(VadInstT));
WebRtcSpl_Init();
self->init_flag = 0;
return (VadInst*)self;
}

并且,它编译成功。

它是如何工作的?

typedef 在一行中组合了前向声明和 typedef。

在C++可以写

struct WebRtcVadInst;            // forward declare a struct
typedef WebRtcVadInst VadInst;   // and introduce an alternate name

在这两种语言中,形成指向未知结构的指针都没有问题,因为所有指向结构(和C++中的类(的指针都需要具有相同的大小。

因此,您显示的代码从不使用结构本身(如果它甚至存在(,而只使用指针(VadInst*)。这在语言方面是可以的。