What is the use of 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_CAPSdirectly and not include _VIDEO_STREAM_CONFIG_CAPS?

+3
source share
3 answers

Pretty simple (at least for me), because some people like to treat user-defined types as "primary" types.

Just as I would not like to say:

struct int i;

I prefer:

VIDEO_STREAM_CONFIG_CAPS vscc;

at

struct VIDEO_STREAM_CONFIG_CAPS vscc;

In fact, I usually completely get rid of the structure tag, preferring:

typedef struct {
    GUID guid;
    ULONG VideoStandard;
    :
} VIDEO_STREAM_CONFIG_CAPS;

The only time I mostly use the tag is whether I should refer to the type inside the type definition itself, for example in linked lists:

typedef struct sNode {
    char paylod[128];
    struct sNode *next;
} tNode;

, tNode , struct sNode ( , struct sNode) 1 , tNode 4, , 3, next, ).

, , , , . - , typedef, .

+5

c struct . typedef struct VIDEO_STREAM_CONFIG_CAPS , . typedef VIDEO_STREAM_CONFIG_CAPS, ++.

struct a {};

struct a A;

typedef struct a {} a;

a A;
+2

In this case, every time a type variable is declared VIDEO_STREAM_CONFIG_CAPS, the following syntax is required:

struct VIDEO_STREAM_CONFIG_CAPS vscc;

With typedef structthis:

VIDEO_STREAM_CONFIG_CAPS vscc;
+2
source

Source: https://habr.com/ru/post/1769470/


All Articles