The following code comes from example abo3.c from Insecure Programming — see also Why cast extern puts
to a function pointer (void(*)(char*))&puts
?:
int main(int argv,char **argc) {
extern system,puts;
void (*fn)(char*)=(void(*)(char*))&system; // <==
char buf[256];
fn=(void(*)(char*))&puts;
strcpy(buf,argc[1]);
fn(argc[2]);
exit(1);
}
Specifically this line:
void (*fn)(char*)=(void(*)(char*))&system;
I think that void (*fn)(char*)
sounds like a lambda, but I know that it's not.
Then, maybe this is only a play with parentheses, where void *fn(char*)
is a declaration of a function and this function is referencing system
? But why does the (char*
) parameter have no name? Is this permitted?
Function pointers in C are variables that can store the memory address of functions and can be used in a program to create a function call to functions pointed by them.
1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code. 2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers. 3) A function's name can also be used to get functions' address.
It declares the variable fn
as a function pointer (to a function that has one argument of type char *
and does not return anything (void
).
This variable is initialised with the address of system
- see http://linux.die.net/man/3/system. As noted from this page this will require the cast as given
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With