Hi,
So I have three similar working functions:
Code:
char *ltrim(char *);
char *rtrim(char *);
char *trim(char *);
And i want to write a function that can test these functions and takes a pointer to function as its argument.
I tried this first
Code:
void test_func(char *(*func)(char *in))
{
printf("in = |%s|\nin.length = %d\nout = |%s|\nout.length = %d\n",
in,
strlen(in),
(*func)(in),
strlen((*func)(in)));
}
But this won't compile and the compiler says 'in' is undeclared.
Then I changed it to this
Code:
void test_func(char *(*func)(char *),char *in)
{
printf("in = |%s|\nin.length = %d\nout = |%s|\nout.length = %d\n",
in,
strlen(in),
(*func)(in),
strlen((*func)(in)));
}
And it seemed to work since i could print out the expected results with this
Code:
test_func(ltrim, str);
So my problem is
1) why the first piece of code won't compile (to be more specific, I'd expected 'in' to be an argument passed to the function that is pointed to. What does the compiler *think* of this variable to be)
2) Is the second piece of code the right way to do this?
Thanks,