102

From sys.c line 123:

void *sys_call_table[__NR_syscalls] = 
{
    [0 ... __NR_syscalls-1] = sys_ni_syscall,
#include <asm/unistd.h>
};

sys_call_table is a generic pointer to arrays, I can see that. However what is the notation:

[0 ... __NR_syscalls-1]

What is the ...?


EDIT:
I learned another C trick here: #include <asm/unistd.h> will be preprocessed and replaced with its content and assigned to [0 ... _NR_syscalls-1].

Amumu
  • 17,924
  • 31
  • 84
  • 131
  • 2
    No, it's not a pointer to an array, it's an array of pointers. A pointer to an array would be declared `void (*sys_call_table)[__NR_syscalls]` – Patrick Schlüter Apr 10 '12 at 14:34
  • @tristopia you're right. What I meant was pointer to arrays, similar to `char *argv[]`. Fixed. – Amumu Apr 10 '12 at 14:48

1 Answers1

91

It is initialization using Designated Initializers.

The range based initialization is a gnu gcc extension.

To initialize a range of elements to the same value, write [first ... last] = value. This is a GNU extension. For example,

 int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };

It is not portable. Compiling with -pedantic with tell you so.

How does it work here?
The preprocessor replaces #include <asm/unistd.h> with its actual contents(it defines miscellaneous symbolic constants and types, and declares miscellaneous functions) in the range based construct, which are then further used for initializing the array of pointers.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • It seems this is not portable. Is is? – Ivaylo Strandjev Apr 09 '12 at 09:32
  • 5
    @Mehrdad does microsoft c compiler complies to c99 standard? I rest my case here...[c99](http://en.wikipedia.org/wiki/C99) – Aftnix Apr 09 '12 at 09:37
  • 3
    @Mehrdad: Actually, only the range based construct of Designated Initializers is an gcc extension.Designated Initializers themselves are allowed by the C standard. – Alok Save Apr 09 '12 at 09:38
  • 2
    @Mehrdad: Sorry, I do not wish to be part of any flame baits,my intention was only to clarify a subtle detail which I thought you misunderstood. – Alok Save Apr 09 '12 at 09:42
  • 2
    @Mehrdad: To be clear, the range construct portable only to gcc (and compilers that implements its extensions), and designated initializers in general are portable only to compilers that support C99 (or at least that particular feature). – Keith Thompson Apr 10 '12 at 07:39
  • @aftnix Which Microsoft C Compiler? They only have a C++ compiler with support for some of the C functionality. – RedX Apr 13 '12 at 10:52
  • @RedX so msft does not want their visual studio tools users to develop stuffs in c? but what about this ? [c++ in kernel mode](http://msdn.microsoft.com/en-us/windows/hardware/gg487420.aspx) – Aftnix Apr 13 '12 at 12:35
  • @aftnix I don't really understand your argument... They do want suff developed in C, that's why they extended their C++ compiler with some C functions. I just don't like it when people complain that a clearly labeled C++ compiler does not conform to C89,C99 standards. – RedX Apr 13 '12 at 12:52
  • @RedX i get your argument, but its still not a modern 'c' compiler. I just hate to initialize structures using {foo,bar,"eat flaming death",....} style :). i lose track of the fields. Its ok that msft endorses c++. they have every right to do that. but still its not a good platform to develop c stuffs. guys developing c stuffs in visual c++ are missing out all the good stuffs in latest standards. – Aftnix Apr 13 '12 at 14:30
  • are there equivalent for none-GNU compiler?? – AminM Oct 10 '13 at 11:04