osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Lies in education [was Re: The "loop and a half"]


Am 12.10.17 um 01:15 schrieb Stefan Ram:
>    Define a function ?g? with a parameter ?x? of type ?int?, so
>    that this function ?g? returns a pointer to another function.
>    This other function has a parameter of type ?char? and returns
>    a double value.

Ok

>    /Without/ a typedef.
> 

And WHY would you do that? Typedefs do not cost money. Try this:

typedef double (*countdown_rate_func) (char letter);
countdown_rate_func select_rate_func(int level);
// I've substituted 'x' by 'level'
// and 'g' by 'select_rate_func' for clarity

I claim that the above is quite clear, it declares a function in a 
Countdown game engine, which, depending on the level, returns a 
different function to rate the weight of the individual letters.

Your exercise results in the line noise
	double (*g(int x))(char)
(thanks to Ben for doing the exercise), which is not extremely bad, but 
still does tell nothing about the intent of that line.

Writing the typedef is easy for humans, stick * in front of the function 
name for a regular declaration and add (). The compiler can then figure 
out the complete type on its own, it's its job, not mine.

	Christian