Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does [ N ... M ] mean in C aggregate initializers?

Tags:

c

linux

kernel

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].

like image 292
Amumu Avatar asked Apr 09 '12 09:04

Amumu


1 Answers

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.

like image 86
Alok Save Avatar answered Sep 29 '22 08:09

Alok Save