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

后端 未结 1 1164
梦如初夏
梦如初夏 2020-12-04 16:26

From sys.c line 123:

void *sys_call_table[__NR_syscalls] = 
{
    [0 ... __NR_syscalls-1] = sys_ni_syscall,
#include 
};
<
1条回答
  •  生来不讨喜
    2020-12-04 16:41

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

    0 讨论(0)
提交回复
热议问题