tags:

views:

87

answers:

3
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 but involving _VIDEO_STREAM_CONFIG_CAPS ?

+2  A: 

In c, you have to put struct in front of a declaration of a struct type. Without this typedef, you'd have to write struct VIDEO_STREAM_CONFIG_CAPS each time you wanted to use it. With the typedef, you can say just VIDEO_STREAM_CONFIG_CAPS as you would in c++.

struct a {};

struct a A;

OR

typedef struct a {} a;

a A;
JoshD
So in c++ I don't have to put `structure` in front of a declaration of a struct type?
justnobody
+4  A: 

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).

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.

paxdiablo
He wants to know why did they do typedef struct _VIDEO { } VIDEO ; AKA, what's the point of the _VIDEO
Kizaru
+ 1 very nice .
JoshD
Is `typedef ` a macro or not?
justnobody
No, `typedef` is a first-class C keyword for defining types.
paxdiablo
@justnobody no its not a macro but you are not actually creating a type, just an alias.
Anders K.
I mean,does `typedef xxx` work like `define xxx`,which is macro in essence?
justnobody
No, `#define` is done in the pre-processing stage and is a simple parameterised text substitution. The compiler stage is unaware of the original text. `typedef`, on the other hand, is done at the compilation stage so that the compiler knows everything about the type.
paxdiablo
Do you mean the compiler knows `_VIDEO_STREAM_CONFIG_CAPS` by `typedef`,but if I use `#define`,it won't know `_VIDEO_STREAM_CONFIG_CAPS`? If that's the case,they functions similar to me since I don't need it to know `_VIDEO_STREAM_CONFIG_CAPS` at all.
justnobody
I mean,except in the nested case.
justnobody
@justnobody, if you use `struct sname {int x;} a` and `#define tname struct sname`, you could achieve a similar effect but the compiler stage will never see `tname`, it'll be replaced with `struct sname` by the preprocessor. So you are right there, but it would be unusual to mix them like that and, as you say, you don't need the structure name at all in this case.
paxdiablo
Thanks!Final question is: I can still use `struct _VIDEO_STREAM_CONFIG_CAPS` if I use `typedef struct _VIDEO_STREAM_CONFIG_CAPS VIDEO_STREAM_CONFIG_CAPS`,right?
justnobody
Yes, you can. The struct bit with the tag creates a definition for the structure and the typedef bit creates the type alias. So you can use either in your code after that.
paxdiablo
Sorry to keep commenting..According to @JoshD's answer,`typedef` is unnecessary any more in c++,say `_VIDEO_STREAM_CONFIG_CAPS` can be used the same way as `VIDEO_STREAM_CONFIG_CAPS`,is that true?
justnobody
Yes, I believe so (though my area of expertise is more C than C++). From memory, structs and classes are very similar in C++ (one difference, the only one I'm aware of, being the default private/public status of members) and I know you don't need to declare an object with `class A a`. In that sense you probably don't need to declare a structure as `struct A a` either. I'll defer to Josh on that one.
paxdiablo
@paxdiablo ,thanks a lot ,you are the MAN!
justnobody
+2  A: 

In that case, each time a variable of type VIDEO_STREAM_CONFIG_CAPS is declared, the following syntax would be required:

struct VIDEO_STREAM_CONFIG_CAPS vscc;

With typedef struct it is:

VIDEO_STREAM_CONFIG_CAPS vscc;
Donotalo
+1 very nice...
JoshD
@JoshD: thanks. yours and pax's are nicer. :)
Donotalo