strange syntax in a device driver

Mar 24, 2009 at 5:54pm
so i am looking through this tutorial on building device drivers in linux and i come accross this code:

struct file_operations memory_fops = {
read: memory_read,
write: memory_write,
open: memory_open,
release: memory_release
};

im not really sure what it all means...
i know what a 'struct' is, its like a class but with different default privileges, but what do 'file_operations' and 'memory_fops' represent? and why is there an '=' sign?

from there, what does the whole
read: memory read,
write:memory_write

etc. thing represent? i have never seen this type of syntax before...

thanks much! here is the full article, i would highly recommend it if you are interested in device drivers:

http://www.freesoftwaremagazine.com/articles/drivers_linux?page=0%2C3
Mar 24, 2009 at 6:12pm
the struct is named file_operations;

an instance of struct file_operations called memory_fops is created;

the instance is initialized using C struct initializer syntax
Mar 24, 2009 at 10:07pm
thanks for the reply jsmith.

hmm... c struct initializer syntax? do you know of a tutorial specific to this kind? i thought the initializer syntax was just defining variables... never seen something like
word: another_word,
word2: another_word2,

etc.
Mar 25, 2009 at 12:55am
I'm not sure, but I believe the syntax might be specific to gcc. There's isn't much to know; here's an example that should explain it all:

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Foo {
   int bar;
   char ch;
   float f;
};

int main() {
    Foo f = {
        bar: 4;
        ch: 'c';
        f: 3.14;
    };
}

Mar 25, 2009 at 2:26am
okay, thanks, that makes more sense. so you think the guy has just defined the memory_fops.read() function as the same as the memory_read() function?
Mar 25, 2009 at 12:28pm
.read is a pointer to a function which has the same signature as memory_read().
[They are all function pointers. This is a common technique you'll find throughout the kernel code.]

Topic archived. No new replies allowed.