LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   confusing array of structs (https://www.linuxquestions.org/questions/programming-9/confusing-array-of-structs-385748/)

kpachopoulos 11-23-2005 03:51 AM

confusing array of structs
 
Code:

static const struct iw_ioctl_description standard_ioctl[] = {
101        [SIOCSIWCOMMIT  - SIOCIWFIRST] = {
102                .header_type    = IW_HEADER_TYPE_NULL,
103        },
..................
264        [SIOCGIWENCODE  - SIOCIWFIRST] = {
265                .header_type    = IW_HEADER_TYPE_POINT,
266                .token_size    = 1,
267                .max_tokens    = IW_ENCODING_TOKEN_MAX,
268                .flags          = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
269        },
270        [SIOCSIWPOWER  - SIOCIWFIRST] = {
271                .header_type    = IW_HEADER_TYPE_PARAM,
272        },
273        [SIOCGIWPOWER  - SIOCIWFIRST] = {
274                .header_type    = IW_HEADER_TYPE_PARAM,
275        },
276 };

This (iw_ioctl_description standard_ioctl[]) is an array of structs. But what is the [SIOCSIWCOMMIT - SIOCIWFIRST] for example? The [define_constant1-define_constant2] in general, where the all define constants are hexadecimal values ? It looks like an array, but what do the 2 operands and the operatot mean? Why do the variables have an "." in front of them? I have been working mainly with Java and i have never seen something like this...
Can somebody help me out?
Thanks

jonaskoelker 11-23-2005 02:13 PM

By the C standard from 1999 (which isn't too widely used even yet), one can initialize `not-atomic object' (i.e. arrays, structs and unions) by writing
Code:

<how to access the member> = <value>
for instance,
Code:

struct {
  int i;
  double j;
} pt = { .j = 2.3, .i = 5 };

is equivalent to

Code:

struct {
  int i;
  double j;
} pt = {5, 2.3};

for array elements, it's

Code:

static int array99[10] = {[1] = [1], [3] = 2};
/* is equivalent to */
static int array89[10] = {0, 1, 0, 2};

Note that (only) because they're static, unspecified values are 0.

So, your specific code just initializes specific members of your array with struct values where specific components are given values.

hth --Jonas

Unrelated: you can also cast brace-notation to structs without it being used in an initializer:
Code:

static struct point
foo() {
    return (struct point) {640, 400};
}



All times are GMT -5. The time now is 05:24 PM.