[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
Re: C Program
hi,
this is true: we all know that the name of a function is a pointer
to itself. well, that being the case, if func() is the function, "func"
is a pointer to func(). and, if (*fp)() = func, and, if you could "call"
the function func() as "func()" (i.e., use the "pointer" "func"), you
could also use another pointer to func - fp for calling it, i.e., you
could use fp() - having the same effect as func().
the linux kernel code makes heavy use of this fact. while fd =
open(...) is a file descriptor identifying a file on the hard disk (or a
device, etc.), sd=socket(...) identifies a socket. now, a read() with fd
activates the filesystem code, read() with sd activates the network
sub-system. internal to the kernel, the file_operations structure
contains pointers to various pointers to functions performing read,
write, etc. of the file.
when an open() is called, a new file_operations structure is
malloc()ed (kalloced), and the file system read(), write() etc.
functions are assigned to these pointers. when a socket() is used, the
same structure is used, but, now, read(), write() etc. are assigned
sock_read(), sock_write(), etc. the actual call of the system call
read() or write() by the user actually gets translated to file_ops
-> read(), file_ops -> write(), etc. i.e, read() and write() is used as
function calls, though they have been declared as "pointers to
functions".
and, if fp() and func() both refer to the same, (*fp)() and
(*func)() too...
arvind.
Rajani K wrote:
> Hi All!!
>
> heres a piece of C code,
>
> #include<stdio.h>
>
> int add(int a, int b)
> {
> printf ( "\n I am there \n" );
> }
>
> main()
> {
> printf("\n%x",add);
> printf("\n%x",*add);
> printf("\n%x",*(*(*(*add))));
> }
>
> The Compiler does not crib. The program gets built and
> the result prints the same value for all the above three printfs.
> What can be the probable reason ?
>
> TIA!!
>
> Cheers
> Rajani
>
> ---------------------------------------------
> LIP is all for free speech. But it was created
> for a purpose. Violations of the rules of
> this list will result in stern action.