Luhko
Luhko

Reputation: 19

What does attribute WCHAR Name[1] means in Windows Kernel?

I was wondering what does this structure means

typedef struct _VOLSNAP_NAME {
  USHORT  NameLength;
  WCHAR   Name[1];
} VOLSNAP_NAME, *PVOLSNAP_NAME;

I'm not understanding why the structure has a NameLength value since the Name length seems to be always 1.
I also don't get why is the WCHAR Name[1] size present : should'nt

WCHAR Name;

be enough ?

Upvotes: -1

Views: 176

Answers (1)

Anders
Anders

Reputation: 101746

Windows has a define named ANYSIZE_ARRAY that you also see in similar structs and its value is 1. Both [1] and [ANYSIZE_ARRAY] are used when the array size is not known at compile time. This scheme was invented long before C99 added support for variable length arrays. [0] would perhaps make more sense but it is not legal syntax.

To work with those structures you often first call the desired API to get the size of the data, then allocate a block of memory big enough and finally call the same API again so it can fill in the data.

See also:

Upvotes: 1

Related Questions