3

Given this struct:

typedef struct _WLAN_AVAILABLE_NETWORK_LIST {
    WLAN_AVAILABLE_NETWORK Network[1];
} *PWLAN_AVAILABLE_NETWORK_LIST;

What does the declaration WLAN_AVAILABLE_NETWORK Network[1] mean?

Trojan
  • 2,256
  • 28
  • 40
david
  • 413
  • 5
  • 20
  • It would be helpful to see what `WLAN_AVAILABLE_NETWORK` is but my first guess it is to emulate passing references without flooding code with `&`, similar to what `gmp` does. – pmr Aug 13 '12 at 15:11
  • 1
    @pmr, http://msdn.microsoft.com/en-us/library/windows/desktop/ms707403(v=vs.85).aspx – chris Aug 13 '12 at 15:12

2 Answers2

9

It looks likely that Network is intended to serve as a flexible array member. By over-allocating the struct by sizeof(Network) * (n - 1) bytes, the library and client code can access past the end of the struct as if the array member was n elements long.

Library code:

PWLAN_AVAILABLE_NETWORK_LIST list = malloc(sizeof(_WLAN_AVAILABLE_NETWORK_LIST)
    + (sizeof(WLAN_AVAILABLE_NETWORK) * (n - 1)));
for (int i = 0; i < n; ++i) {
    list->Network[i] = ...;
}

Client code:

for (int i = 0; i < n; ++i) {
    do_something(list->Network[i]);
}
Community
  • 1
  • 1
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • 1
    I was about to type this. The function [WlanGetAvailableNetworkList](http://msdn.microsoft.com/en-us/library/windows/desktop/ms706749.aspx) only ever gives the caller back a pointer to the struct, which has a count of the items in the array as another member. – Matt K Aug 13 '12 at 15:16
1

typedef struct _WLAN_AVAILABLE_NETWORK_LIST { declaring a struct named _wlan...list

WLAN_AVAILABLE_NETWORK Network[1]; assumes a struct called WLAN_AVAILABLE_NETWORK is declared somewhere. It is an array of length 1 (pointless) and called Network.

} *PWLAN_AVAILABLE_NETWORK_LIST; instantly creates a (pointer) variable of this struct called pwlan...list

Raekye
  • 5,081
  • 8
  • 49
  • 74
  • yeah may be pointer variable can points to some variable memory later by allocating, and by using that array variable we can use accordingly.. thank you – david Aug 24 '12 at 10:55