Click to See Complete Forum and Search --> : c pointers


phyte
01-22-2003, 10:42 PM
Could somebody clarify what this code is doing?

void (*fn)(char*)=(void(*)(char*))&system;

Are there any good sites that i could study so that i could have a better understanding of the above code??

The Kooman
01-22-2003, 11:20 PM
Its making fn "point" to the function "system".

fn is declared to be a pointer (*fn) to a function ((*fn)()) that takes a "char *" argument ((*fn)(char *)) and returns "void" (void (*fn)(char *)).

the function "system" is being typecast to such a function type, .i.e pointer ((*) to a function ((*)()) that takes "char *" ((*)(char *)) and returns "void" (void (*)(char *))

I've tried to make the appropriate sections bold!

The "&" is not really necessary for functions since the name of a function is the address of the function.

Does that make things any clearer ;)?

truls
01-23-2003, 10:15 AM
If you could get hold of the book "C - Traps and Pitfalls" there's an excellent discussion on this. But do a google on "function pointers in C" or something and you'll find lots.

For an explanation I'd have to say that The Kooman has done that to perfection ;)

phyte
01-24-2003, 12:01 AM
Thanks guys,

I totally understand it now, It looked really obfuscated when i first looked at it and i had not yet encountered function pointers at all.

What a neat idea they are :).Back to code.

Thanks again.Great explaination

goon12
01-24-2003, 04:35 PM
obfuscated - Great word! :D