1

Possible Duplicate:
What means the dot before variable name in struct?

const struct file_operations generic_ro_fops = {
    .llseek     = generic_file_llseek,
    .read       = do_sync_read,
    // ....other stuffs
};

my question is :

1) what is the meaning of .llseek and how to use ....the file_operations struct definition is below:

2) can I just say : llseek = generic_file_llseek ; in the struct above to let the pointer llseek points to generic_file_llseek without putting the dot . before llseek? //sorry for my poor english

struct file_operations {
    loff_t (*llseek) (struct file *, loff_t, int);
    ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
    //....other stuffs
}
Community
  • 1
  • 1
Hank Zhang
  • 89
  • 5

4 Answers4

2

It's a special structure initialization syntax that has been introduced in c99.

When you have .llseek = generic_file_llseek, inside your structure initializer, this means you are initializing this particular member regardless of its offset relative to the structure beginning.

Alternatively you can use an initialization without designators, but you will have to put the initializers in the exact order as the corresponding fields in the structure are declared.

const struct file_operations generic_ro_fops = {
    generic_file_llseek, // this initializes llseek member
    do_sync_read, // this initializes read member
    // ....other stuffs
};
Maksim Skurydzin
  • 10,301
  • 8
  • 40
  • 53
1

This is called designated initializers. You are initializing exact member/field of the structure.

2.can i just say : "llseek = generic_file_llseek ;" in the struct above to let the pointer llseek points to generic_file_llseek without putting the dot '.' before llseek?

No

You can do something like this to initialize members:

const struct file_operations generic_ro_fops = {
    generic_file_llseek,
    do_sync_read,
    // ....other stuffs
};

but in this case you will have to initialize everything in order like members are placed inside structure. Designated initializers are very helpful when you want to initialize just some of structure fields.

You can use something like this outside structure:

generic_ro_fops.llseek = generic_file_llseek;
codewarrior
  • 1,269
  • 1
  • 9
  • 14
0

Meaning

1) what is the meaning of .llseek and how to use ....the file_operations struct definition is below:

It is a C99 feature: designated initializers. With the . you can initialize a given field of your structure. Here, it is necessary to initialize your structure cause of the const qualifier.

If lseek and read are in this order in the structure definition, you can ommit this designator.

const struct file_operations generic_ro_fops = { 
    generic_file_llseek, 
    do_sync_read 
};

This last method is not very maintenable (if you change the order of your structure's fields, you have to change all your initialization code), therefore the method with designated iniitializers is better, here.

Other manner

2) can I just say : llseek = generic_file_llseek ; in the struct above to let the pointer llseek points to generic_file_llseek without putting the dot . before llseek? //sorry for my poor english

No, you can't, because it is not the right syntax.

References

C11 (n1570), § 6.7.9 Initialization

If a designator has the form

. identifier

then the current object (defined below) shall have structure or union type and the identifier shall be the name of a member of that type.

md5
  • 23,373
  • 3
  • 44
  • 93
0

The meaning of the '.' in that struct is called a designated initializer. In the struct, each '.' represents member of the struct which needs to be initialized, thus being called a designator.

Florin Stingaciu
  • 8,085
  • 2
  • 24
  • 45