-1
static struct option long_options[] = {
     {"help", no_argument, 0, 'h'},
     {"config",    required_argument, 0, 'c'},
     {"sampleIn",  required_argument, 0, 's'},
     {"submitDir", required_argument, 0, 'd'},
     {"numEvents", required_argument, 0, 'n'},
     {"official",  no_argument, 0, 'o'},
     {"noSubmit", no_argument, 0, 'x'},
     {0,0,0,0}
};

at the code I pasted, there seems like to declared a struct named "option",but I am confused about the array declared after the struct name, what does this code do?

Appreciate for your answers, could you please have a brief explanation about the struct option?It seems like a struct already defined in some header file.

spring cc
  • 937
  • 1
  • 10
  • 19
  • 3
    Do not post pictures of code. *Ever.* – Nicol Bolas May 09 '16 at 02:36
  • 3
    Please update our question so the code is part of the text of the question. There's no reason to post a link to code (whether an image or not) that is only 10 line long or so. Just put it in the question. – Michael Burr May 09 '16 at 02:39
  • It's declaring an array of 8 option structs. The array is named long_options, so long_options[0] is the first struct, initialized with the arguments in the first set of braces, long_options[1] is the second struct in the array, and so on. – Jeremy Friesner May 09 '16 at 02:39
  • Voted for re-opening, as OP edited the question. – vsoftco May 09 '16 at 02:56
  • @spring cc, see the updated answer for your last edit. – vsoftco May 09 '16 at 04:51

3 Answers3

2

I fully agree with @Nicol Bolas, post code, not pictures. But, I'll tell you what it means, as this is your first post on SO and may not be familiar with the rules (although you should read the guide and also how to write an MCVE).

Your code defines an array of struct option and brace-initializes it. The array is called long_options. The length of it is determined at compile time, in your case it has length 8. It is marked static, i.e. it is visible only in the current compilation unit, or, more technically, it has internal linkage (this unless you define it inside a function, in which case it is visible across function invocations).

PS: If this is C++ code (and not C), then you can omit struct from the definition, and simply use

static option long_options[] = {...};

In C, you need to use struct as part of the type though. In C++, it is optional.

Regarding your last edit: yes, the type struct option was declared before (most likely in a header file), as otherwise the declaration would've looked like

// defines a new struct type and brace-initializes an array of that type
static struct option{...} long_options[] = {...}; 
Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252
0

This is just declaring and initialising an array of these structs.

The struct must have been elsewhere defined, so now an array of them is created and each element (a struct) is being initialised with the shown values.

0

The last option has its values all set to 0 (or, in the case of pointers, NULL). Whatever code is processing this array is going to expect that this sentinel struct will be used to mark the end of the array, much in the same way '\0' does for strings (char*).

Technically, you could get the size of this array as it is with sizeof(long_options) / sizeof(option). But not if you pass it into a function expecting a option*; at that point, it's anyone's guess as to how big the array actually is.

JesseTG
  • 2,025
  • 1
  • 24
  • 48