Search code examples
clinux-device-driverdevice-tree

C - Meaning of empty terminator in struct array initialization


While going through PCIe driver, I see this type of struct array initialization

static struct pci_device_id DWC_ETH_QOS_id[] = {
    { PCI_DEVICE(VENDOR_ID, DEVICE_ID), },
    { },
}

I found similar code in device tree of_device_id initializer as well:

static const struct of_device_id rh850_match_table[] = {
    { .compatible = "renesas,rh850" },
    { }
};

Why is there an empty {} at the end?

Thanks for the help!


Solution

  • The ISO C grammar requires an initialiser list to be non-empty, however, some compilers (e.g. GCC) permit this.

    Here is a discussion on the topic.

    The answer to what happens if its empty (do you get as-if-zero if there is no = {...} part) is "yes if the object has static duration, no if not".

    Personally I initialize with values I know and do not depend on undefined behaviour. It could limit code portability but I suspect that is not an issue in the community where this practice is common (Linux drivers where GCC is the default compiler.