I'm a little confused by the textbook I'm using compared to examples, SO answers, and tutorials I've found online.
The code from the book declares two function pointers but never assigns a value to them, and it doesn't use *
in the declaration. The code runs fine and compiles without warning, so I'm a little confused.
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
void f(int), g(int);
signal(SIGUSR1, f);
signal(SIGUSR2, g);
while(1)
sleep(1);
}
void f(int signum)
{
printf("Recieved signal %d: SIGUSR1\n", signum);
}
void g(int signum)
{
printf("Received signal %d: SIGUSR2\n", signum);
exit(SIGUSR2);
}
The code that I've found online all looks similar to this, with pointer syntax *
and an explicit assignment of a function address to the function pointers:
#include <stdio.h>
void fun(int a)
{
printf("Value of a is %d\n", a);
}
int main()
{
void (*fun_ptr)(int) = &fun;
(*fun_ptr)(10);
return 0;
}
Is there more than one correct way to declare a function pointer?
Why does the code from the book work?
Is one version "more correct" than the other?