Anton
Anton

Reputation: 103

C Struct syntax question

This question appeared when I recently opened a rather old driver for my raid device. To be able to compile the driver for a Linux system, I started to investigate on all those errors I got on output. And I came across this kind of syntax used in the driver sources:

struct file_operations t3_fops = {
        owner:                  THIS_MODULE,
        ioctl:                  ft_ioctl,
        fasync:                 ft_fasync,
        open:                   ft_open,
        release:                ft_release
};

So guys, could you help me to understand what does ":" mean? Is this C syntax at all? I know there is a bit field definition, but this looks rather different to me.

Upvotes: 8

Views: 797

Answers (2)

sigjuice
sigjuice

Reputation: 29759

This syntax for initializing structure members is called a designated initializer. The : is older GCC-specific syntax. This is documented in the GCC manual.

Upvotes: 6

user25148
user25148

Reputation:

This is C99 struct initialization syntax. owner, ioctl, etc, are names of fields in the struct, and THIS_MODULE, ft_ioctl, etc, are the values. This is effectively doing the following, except at compile time:

struct file_operations t3_fops;
t3_fops.owner = THIS_MODULE;
t3_fops.ioctl = ft_ioctl;
...

The new syntax is nice, because it makes the initialization work regardless of the order of the struct fields.

Upvotes: 6

Related Questions