简体   繁体   English

使用带有结构的typedef关键字

[英]using the typedef keyword with a struct

What exactly is the purpose of using a typedef together with a struct and giving it two names, often one in capital letters? 将typedef与结构一起使用并给它两个名称(通常以大写字母命名)的目的到底是什么?

Today I was looking at a winsock tutorial and came across the struct definition below. 今天,我正在看一个winsock教程,并遇到了下面的struct定义。

// IPv4 AF_INET sockets:
struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr   sin_addr;     // see struct in_addr, below
    char             sin_zero[8];  // zero this if you want to
};

typedef struct in_addr {
  union {
    struct {
      u_char s_b1,s_b2,s_b3,s_b4;
    } S_un_b;
    struct {
      u_short s_w1,s_w2;
    } S_un_w;
    u_long S_addr;
  } S_un;
} IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR;


struct sockaddr {
    unsigned short    sa_family;    // address family, AF_xxx
    char              sa_data[14];  // 14 bytes of protocol address
};

What exactly does this line do? 这条线到底是做什么的?

IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR; IN_ADDR,* PIN_ADDR,FAR * LPIN_ADDR;

What exactly is the purpose of using a typedef together with a struct and giving it two names, often one in capital letters? 将typedef与结构一起使用并给它两个名称(通常以大写字母命名)的目的到底是什么?

There are three primary components to 有三个主要组成部分

 typedef struct in_addr { // members ... } IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR; 
  • several identifiers that are being declared: IN_ADDR , PIN_ADDR , and LPIN_ADDR . 声明了几个标识符: IN_ADDRPIN_ADDRLPIN_ADDR As far as C is concerned, there is no significance to the fact that these are capitalized; 就C而言,将它们大写的事实是没有意义的。 presumably that is an aspect of the chosen coding style. 大概是所选编码样式的一个方面。

  • the types being declared for those identifiers, respectively struct in_addr , struct in_addr * and struct in_addr FAR * . 为这些标识符声明的类型分别为struct in_addrstruct in_addr *struct in_addr FAR * The type declaration additionally incorporates a definition of type struct in_addr , which is optional because a struct tag ( in_addr ) is given. 类型声明还包含类型struct in_addr的定义,该定义是可选的,因为给出了struct标记( in_addr )。 The definition could have instead been given separately or not at all: 该定义可以改为单独给出或根本不给出:

     typedef struct in_addr IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR; struct in_addr { // members ... }; 
  • the storage class of the declarations, typedef , which indicates that instead of those identifiers referring to objects with associated storage, they serve as aliases for their declared types. 声明的存储类typedef ,指示它们代替引用具有关联存储的对象的标识符,而是用作其声明类型的别名。

Overall, then, the statement declares IN_ADDR , PIN_ADDR , and LPIN_ADDR as aliases for three different, but related, types. 总的来说,该语句将IN_ADDRPIN_ADDRLPIN_ADDR声明为三种不同但相关的类型的别名。

This creates the types IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR , which are synonymous with struct in_addr which in turn refers to the structure definintion given. 这将创建类型IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR ,它们与struct in_addr同义,后者又引用了给定的结构定义。 This practice is is useful when you want to typedef a structure with a self reference, but don't want to add the struct keyword before each declaration:: 当您想使用自引用对结构进行typedef定义,但又不想在每个声明之前添加struct关键字时,此做法非常有用:

typedef struct _SomeStruct_t{
    int                    someVal;
    struct _SomeStruct_t  *pNext;
} SomeStruct_t;

SomeStruct_t  gSomeStruct;

You can't set pNext to be a pointer to SomeStruct_t because it hasn't been defined at that point, so you use struct _SomeStruct_t instead. 您不能将pNext设置为指向SomeStruct_t的指针,因为当时尚未定义它,因此可以改用struct _SomeStruct_t

You use typedef to create an alias for the struct type, so that you don't have to type it out fully the next time you want to use it. 您可以使用typedef为结构类型创建别名,以便下次使用时不必完全键入它。 If you didn't have the typedef, any time you want to use in_addr you would have to declare it as struct in_addr . 如果没有typedef,则in_addr要使用in_addr时都必须将其声明为struct in_addr The typedef allows you to declare it as IN_ADDR . 使用typedef可以将其声明为IN_ADDR When the typedefed name does not have an asterisk, it creates an alias for the type, and a name preceded by an asterisk, as in *IN_ADDR creates an alias for a pointer to the type, so you can write PIN_ADDR instead of struct in_addr* . 当类型定义的名称没有星号时,它会为该类型创建一个别名,并在名称前加上一个星号,例如*IN_ADDR为该类型的指针创建一个别名,因此您可以编写PIN_ADDR而不是struct in_addr*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM