[英]What's the benefit of a typedef in C?
typedef struct _VIDEO_STREAM_CONFIG_CAPS
{
GUID guid;
ULONG VideoStandard;
SIZE InputSize;
SIZE MinCroppingSize;
SIZE MaxCroppingSize;
int CropGranularityX;
int CropGranularityY;
int CropAlignX;
int CropAlignY;
SIZE MinOutputSize;
SIZE MaxOutputSize;
int OutputGranularityX;
int OutputGranularityY;
int StretchTapsX;
int StretchTapsY;
int ShrinkTapsX;
int ShrinkTapsY;
LONGLONG MinFrameInterval;
LONGLONG MaxFrameInterval;
LONG MinBitsPerSecond;
LONG MaxBitsPerSecond;
} VIDEO_STREAM_CONFIG_CAPS;
Why not define structure VIDEO_STREAM_CONFIG_CAPS
directly instead of involving _VIDEO_STREAM_CONFIG_CAPS
? 为什么不直接定义
structure VIDEO_STREAM_CONFIG_CAPS
而不是涉及_VIDEO_STREAM_CONFIG_CAPS
?
Quite simply (at least for me) because some people like to be able to treat user defined types as "primary" types. 很简单(至少对我来说)因为有些人喜欢能够将用户定义的类型视为“主要”类型。
Just like I wouldn't like to have to say: 就像我不想说:
struct int i;
I prefer: 我更喜欢:
VIDEO_STREAM_CONFIG_CAPS vscc;
to: 至:
struct VIDEO_STREAM_CONFIG_CAPS vscc;
In fact, I usually get rid of the structure tag altogether, preferring: 事实上,我通常完全摆脱结构标签,更喜欢:
typedef struct {
GUID guid;
ULONG VideoStandard;
:
} VIDEO_STREAM_CONFIG_CAPS;
The only time I genarally use the tag is if I have to refer to the type within the type definition itself, such as in linked lists: 我唯一一次使用标签是因为我必须在类型定义本身中引用类型,例如在链表中:
typedef struct sNode {
char paylod[128];
struct sNode *next;
} tNode;
That's because, at the time of creating the definition, tNode
doesn't yet exist but struct sNode
does (you can think of it as a simple sequencing thing if that makes it easier - struct sNode
gets created on line 1 above, tNode
on line 4, which means on line 3 where you create the next
pointer, you have to use the structure name). 那是因为,在创建定义时,
tNode
还不存在,但是struct sNode
确实存在(你可以把它想象成一个简单的排序的东西,如果这样做更容易 - struct sNode
在上面的第1行创建, tNode
在线4,这意味着你在第3行创建next
指针,你必须使用结构名称)。
In the case you cite, the structure tag is superfluous at least in the code shown. 在您引用的情况下,结构标记至少在显示的代码中是多余的。 Whether some other piece of the code declares a variable with the structure name rather than the typedef name is unclear.
是否其他部分代码声明了具有结构名称而不是typedef名称的变量尚不清楚。
In c, you have to put struct
in front of a declaration of a struct type. 在c中,您必须将
struct
放在struct类型的声明之前。 Without this typedef, you'd have to write struct VIDEO_STREAM_CONFIG_CAPS
each time you wanted to use it. 如果没有这个typedef,每次要使用它时都必须编写
struct VIDEO_STREAM_CONFIG_CAPS
。 With the typedef, you can say just VIDEO_STREAM_CONFIG_CAPS
as you would in c++. 使用typedef,您可以像在c ++中一样说出
VIDEO_STREAM_CONFIG_CAPS
。
struct a {};
struct a A;
OR 要么
typedef struct a {} a;
a A;
In that case, each time a variable of type VIDEO_STREAM_CONFIG_CAPS
is declared, the following syntax would be required: 在这种情况下,每次声明
VIDEO_STREAM_CONFIG_CAPS
类型的变量时,都需要以下语法:
struct VIDEO_STREAM_CONFIG_CAPS vscc;
With typedef struct
it is: 使用
typedef struct
它是:
VIDEO_STREAM_CONFIG_CAPS vscc;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.