0

I am reading files of xv6 kernel and I cannot understand what the following means:

static int (*syscalls[])(void) = {
 [SYS_fork]    sys_fork,
 [SYS_exit]    sys_exit,
 [SYS_wait]    sys_wait,
 [SYS_pipe]    sys_pipe,
...

}

Can someone explain this to me? Especially what square brackets (e.g [SYS_fork]) mean. Thank you

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
RUstar
  • 37
  • 5

2 Answers2

1

That code is making an array of function pointers, using an old alternative GNU extension for designated initialization.

Designated initializations is a feature that was added to C in C99 that lets you specify which array index to assign a specific value for arrays, so they need not be in order. The same feature exists for struct initializations where you can specify the specific field to assign a given value to.

The C99 syntax for array designated initializations is [index] = value. This code in particular though is using an older alternative syntax from GCC, which as per this document has been obsolete since GCC 2.5, in which there is no equals sign used.

In syscall.c the indices are specified using macros defined in syscall.h, the first of which is defined to 1 in syscall.h, et.c.

remmy
  • 157
  • 3
  • 15
0

This is most likely a non-standard way of initializing an array of function pointers. The identifiers SYS_fork etc. are very likely macros or enum constants specifying the element index.

Another possibility is that this is not a C file, but is turned into a syntactically valid C file using some filtering tool prior to compilation.

Jens
  • 69,818
  • 15
  • 125
  • 179