On Tue, Apr 23, 2002 at 01:08:41PM -0400, Dan Erickson wrote: > I have asked this question before, but I didnt get a response from > somebody who understood my question correctly. So.. I will try showing a > couple more examples. Bummer. I thought I did a good job describing my answer before. :) > static ssize_t wdt_write(struct file *file, const char *buf, size_t count, > loff_t *ppos) > { /* stuff */ > } > > > What has me confussed is.... what gives this function the arguments so it > can function properly. Ie) as bar is the arg to atoi() earlier. Dan, with this particular function, wdt_write() isn't called directly anywhere in kernel code. (At least, not in the kernel tree I'm using, a 2.4.17 fork.) Instead, the write: wdt_write; line in the wdt.c file causes gcc to put a pointer to the wdt_write() function in the wdt_fops structure (of type struct file_operations). Somewhere in kernel code, someone else is going to wind up calling wdt_fops->write(), and they will pass that function the struct file*, char *, size_t, and loff_t that the wdt_write() function expects. Perhaps a simpler example would help bridge the gap. Consider the following (untested and uncompiled) code: #include <stdio.h> int func(int); int (*fp)(int); int func(int a) { return a; } int main(int argc, char *argv[]) { int value; fp = &func; value = fp(314159); /* line a */ printf("%d\n", value); /* this will print 314159\n to the console */ } The kernel code is similar, but different; a function pointer to wdt_write() is stored in a struct, and then somewhere along the line someone will call wdt_write() using the function pointer in the struct. For more details, check out pages 118--121 in K&R 2. (They don't mention in those pages that the function can be called through the function pointer without de-referencing the pointer; they would have written line a as: value = (*fp)(314159); I recall reading that function pointers don't need to be dereferenced in this manner, and that no amount of indirection will require dereferencing, but I don't recall now where I read that. Sorry.) I hope this has helped. If it hasn't, we'll need a whiteboard to try again. :) -- http://immunix.org/
Attachment:
pgp00068.pgp
Description: PGP signature