Home > Software design >  struct is it declaration or defination or some type of Object Oriented behavior in kernel source
struct is it declaration or defination or some type of Object Oriented behavior in kernel source

Time:11-15

In r8169_main.c in linux kernel source there is a struct like this

#define KBUILD_MODNAME "foo"

static struct pci_driver rtl8169_pci_driver = {
    .name       = KBUILD_MODNAME,
    .id_table   = rtl8169_pci_tbl,
    .probe      = rtl_init_one,
    .remove     = rtl_remove_one,
    .shutdown   = rtl_shutdown,
    .driver.pm  = pm_ptr(&rtl8169_pm_ops),
};

I like to in first member .name but there is no data type specified and there is . before name like .name = KBUILD_MODNAME, what is it called. is this name member defined somewhere else what this static struct means I did not seen this type of struct before. Can any one please tell what is this and its even assigned a value in struct declaration???

can I also create array of this type of struct?

CodePudding user response:

what is it called.

name is a member of structure struct pci_driver.

The initialization form is called designated initialization. The part .name = something is designator.

is this name member defined somewhere else

Yes, in include/linux/pci.h.

what this static struct means I did not seen this type of struct before

The line defines a variable rtl8169_pci_driver of type struct pci_driver. The static keyword modifies variable lifetime - it means the variable is valid for the whole duration of the program.

static struct pci_driver rtl8169_pci_driver = {...}
                                            ^^^^^^^ - initialization
                         ^^^^^^^^^^^^^^^^^          - variable name
       ^^^^^^^^^^^^^^^^                             - variable type
^^^^^                                               - storage specifier
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^          - variable declaration
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - variable definition

Research https://en.cppreference.com/w/c/language/declarations https://en.cppreference.com/w/c/language/struct_initialization https://en.cppreference.com/w/c/language/storage_duration

can I also create array of this type of struct?

You can create arrays of struct pci_drivers.

static struct pci_driver example_array[20] = {
   { .name = initilization_for_first_element, ... },
   { .name = initilization_for_second_element, ... },
   ... etc.
};
  • Related