4

I came across an unfamiliar array initialization syntax in the code of xv6. Original code is in syscall.s

static int (*syscalls[])(void) = {
[SYS_fork]    sys_fork,
[SYS_exit]    sys_exit,
[SYS_wait]    sys_wait,
// ...
[SYS_halt]  sys_halt,
};

I ran my sample code to confirm this

const char* mystrings[] = {
   [2] "this",
   [1] "is",
   [0] "cool",
};


int main (int argc, char const* argv[])
{
   printf("%s %s %s\n",mystrings[0],mystrings[1],mystrings[2]);

        return 0;
}

Output was

cool is this

What is this kind of array initialization in C code? Where can i ( find documentation for this? ( I compiled with gcc 4.6.3 )

1 Answer 1

3

Designated initializers, part of C99. See for example the GCC documentation. The current standard reference is C11 6.7.9.

However, GCC offers a few extensions to this syntax, such as omitting the = sign, or ranges.

Sign up to request clarification or add additional context in comments.

1 Comment

@ShafikYaghmour: Well spotted, thanks a lot! I incorporated that in the post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.