1

I'm recently taking Operating System course, where we study xv6. During practice lab I ran into the code like below:

// xv6_public/syscall.c
#include "defs.h"
#include "syscalls.h"
...

extern int sys_fork(void);

...

static int (*syscalls[])(void) = {
    [SYS_fork]    sys_fork,
    ...
};

I didn't recognize the syntax used above at all at first. After searching the net I found that the syntax is called Lambda function, but I still have questions:

  1. Many articles say it is available to use Lambda only with C++. Is it able to use Lambda in C? Is syntax the same as that of C++? (the source files are all .c, probably compiler is g++)
  2. Would someone briefly explain the code above? I can understand that it is function pointer array where each element is lambda function. Or please recommend nice reference to read.
cadenzah
  • 928
  • 3
  • 10
  • 23

1 Answers1

2

That's not C++ lambdas, instead it's standard C99 array initialization using designated initializers.

The value inside the square brackets (e.g. [SYS_fork]) is a compile-time integer constant and designates the index in the array for the initialization.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621