I accidentally forgot to add (int) when defining a function pointer, and my program still worked. I wold like to know if there is any case where it would not work. my code:
#include <stdio.h>
void f1(int var)
{
printf("this is f1 and var is: %d\n", var);
}
void f2(int var)
{
printf("this is f2 and var is: %d\n", var);
}
void f3(int var)
{
printf("this is f3 and var is: %d\n", var);
}
typedef void (*f_ptr)(int);
// pq eu poderia escrever: typedef void (*f_ptr)(); e o programa funcionaria normalmente?
typedef int n_casa;
int main()
{
f_ptr ptr[] = {f1, f2, f3};
int c = 0;
while (c < 3)
{
ptr[c](c);
++c;
}
return 0;
}
both typedef void (*f_ptr)(int);
and typedef void (*f_ptr)();
worked in my program.
They are different.
typedef void (*f_ptr)(int)
declares a funciton pointer that takes only one int
argument, and returns nothing.
While for typedef void (*f_ptr)()
, the function pointer takes an UNSPECIFIED number of arguments, and returns nothing.
According to the SEI CERT C Coding Standard, it is recommended to explicitly specify void
when a function accepts no arguments.