Search code examples
clinuxopen-source

What's the mechanism behind nameless brackets in left hand assignment as seen in initramfs?


I was browsing the Linux source and I came across an unfamiliar piece of code on line 400 of initramfs. I get that the names in the brackets are states from an enum on 188. It also seems to me that this is a default array setting function pointers ( I could be wrong).

I don't understand what the brackets are for, what's an example use case?I also found the equal signs confusing. Additionally, I'd like to know if there's a general name for the pattern the code implements.

static __initdata int (*actions[])(void) = {
    [Start]     = do_start,
    [Collect]   = do_collect,
    [GotHeader] = do_header,
    [SkipIt]    = do_skip,
    [GotName]   = do_name,
    [CopyFile]  = do_copy,
    [GotSymlink]    = do_symlink,
    [Reset]     = do_reset,
};

Side; Apologies for a naive/general question, I'm trying to broaden my knowledge of C. I searched for and couldn't find a comprehensive commented version of the source in english.


Solution

  • The variable actions is an array, and using square-brackets [] is used for designators, i.e. telling the compiler what index of the array is being initialized.

    From the C11 specification (§6.7.9/6):

    If a designator has the form

    [ constant-expression ]
    

    then the current object (defined below) shall have array type and the expression shall be an integer constant expression. If the array is of unknown size, any nonnegative value is valid.

    Enumeration identifiers are counted as a constant-expression.


    This means you can do something like this stupid example:

    #include <stdio.h>
    
    int main(void)
    {
        int array[] = {
           [2] = 0,    // Initialize array[2] to 0
           [0] = 2,    // Initialize array[0] to 2
           [1] = 1     // Initialize array[1] to 1
        };
    
        for (size_t i = 0; i < (sizeof array / sizeof array[0]); ++i)
            printf("array[%d] = %d\n", i, array[i]);
    
        return 0;
    }
    

    The above program should print

    array[0] = 2
    array[1] = 1
    array[2] = 0