I have a simple test code for Function Pointer:
void PrintHello(char *name)
{
printf("Hello %s\n", name);
}
int main(int argc, const char * argv[])
{
//ptr is a function pointer
void (*ptr)(char*);
ptr = PrintHello;
ptr("world");
return 0;
}
The code build & run successfully. The "Hello world" string is printed out.
But, what I don't understand is, the function PrintHello(char*)
accepts a pointer to string as argument. But my code calls this function through Function Pointer ptr("world")
, in which I directly passed the string "world" to the function, not a pointer to string. Why it works?