LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2005, 03:51 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
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

Last edited by kpachopoulos; 11-23-2005 at 04:20 AM.
 
Old 11-23-2005, 02:13 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
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};
}

Last edited by jonaskoelker; 11-23-2005 at 02:20 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy structs in c alaios Programming 10 09-10-2005 02:31 PM
structs & scope jnusa Programming 6 11-16-2004 05:33 AM
pointers to structs in C spuzzzzzzz Programming 5 06-03-2004 05:41 PM
static structs? simbo Programming 3 02-05-2004 04:00 AM
Self referential structs in C? MadCactus Programming 14 01-28-2004 05:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration