Re: Function Pointers



Richard Heathfield <rjh@xxxxxxxxxxxxxxx> writes:

bwaichu@xxxxxxxxx said:

On Jan 21, 11:42 am, Keith Thompson <ks...@xxxxxxx> wrote:


If you feel the need for a generic pointer-to-function type, you
should probably create a typedef for it.

Can you walk me through the typedef'ing of a function pointer? I'm
not clear on the syntax, and the examples posted early confuse me. Is
the purpose of the typedef to make the code more readable?

Others have shown you how to typedef a function pointer type. My own take
is that it's better not to do this, because I don't like hiding pointers
in typedefs.

Ditto. In fact I posted such an example in my reply to the OP! I was
not going to bother saying this except that I find I have two points to
add:

So I would typedef the function type itself:

typedef int myfunctiontype(int, char **);

and then I'd define a pointer to it using a good old out-in-the-open *,
like this:

myfunctiontype *entrypoint = main;

(It's true that myfunctiontype foo; is an error because you can't create
objects of function type, but the answer to that is easy: Don't Do
That.)

You can, however, *declare* functions using it. I have done that once or
twice in code that looked a bit like this:

typedef int op_type(int, int);

extern op_type op_add, op_sub, op_mul, op_div;

op_type *op_table[] = { op_add, op_sub, op_mul, op_div };

It does not buy you much, but it is slightly neater than some alternatives.

I accept that this way may not be everybody's slice of apple pie. Style is
often a matter of taste.

I also prefer to see the "pointerlyness" of pointers, but when it is
hidden I think it matters least with function pointers, simply because
the use makes the type so clear. As soon as you see

op_table[0](a, b);

you know what is going on. There are operations on data pointers that
don't make it obvious that they are pointers (like addition) but you
can so little with a function pointer that I worry less when I see a
"hidden pointer" typedef.

--
Ben.
.



Relevant Pages

  • Re: structures, structures and more structures (questions about nested structures)
    ... >>>typedef union { ... How do I calculate the size of the structure MotherStruct ... of all members, including pointers. ... How can sizeof know how much memory you have allocated for the char* ...
    (comp.lang.c)
  • Re: Question about a struct declaration
    ... typedef struct dummy *dummy; ... If want to use the type as a pointer, then hiding the fact that it is a pointer in a typedef is indeed a pretty useless idea. ... A classic example would be the interface of 'pthreads' library, where 'pthread_t', 'pthread_mutex_t' etc. might easily be pointers. ... work in case when you need to self-refer to the struct type from inside ...
    (comp.lang.c)
  • Re: MEX: using malloc with C++ in C wrapper, error conversion from void to myType
    ... Somehow I was thinking of pointers as being just pointers, with an otherwise nebulous definition, but yes, pointers are variables containing an address. ... Microvolt is a double, fyi. CTypes.h contains: ... typedef double Hertz; ... itmp = mxMalloc); ...
    (comp.soft-sys.matlab)
  • Re: Function Pointers
    ... is that it's better not to do this, because I don't like hiding pointers ... So I would typedef the function type itself: ... Pointers to functions are fundamentally different from pointers to ... object pointers behind a typedef is Evil, ...
    (comp.lang.c)
  • Re: Typdef pointers to structs or not?
    ... (This is straight from memory so it might not even compile.) ... The use case is AST representation which makes heavy use of pointers to ... structs ... modify these structs so either the whole typedef will be in a .h file ...
    (comp.lang.c)

Loading