Preprocessor "macro function" vs. function pointer - best practice?

Posted by Dustin on Stack Overflow See other posts from Stack Overflow or by Dustin
Published on 2010-05-15T00:02:03Z Indexed on 2010/05/15 0:14 UTC
Read the original article Hit count: 229

Filed under:
|

I recently started a small personal project (RGB value to BGR value conversion program) in C, and I realised that a function that converts from RGB to BGR can not only perform the conversion but also the inversion. Obviously that means I don't really need two functions rgb2bgr and bgr2rgb. However, does it matter whether I use a function pointer instead of a macro? For example:

int rgb2bgr (const int rgb);

/*
 * Should I do this because it allows the compiler to issue
 * appropriate error messages using the proper function name,
 * not to mention possible debugging benefits?
 */
int (*bgr2rgb) (const int bgr) = rgb2bgr;

/*
 * Or should I do this since it is merely a convenience
 * and they're really the same function anyway?
 */
#define bgr2rgb(bgr) (rgb2bgr (bgr))

I'm not necessarily looking for a change in execution efficiency as it's more of a subjective question out of curiosity. I am well aware of the fact that type safety is neither lost nor gained using either method. Would the function pointer merely be a convenience or are there more practical benefits to be gained of which I am unaware?

© Stack Overflow or respective owner

Related posts about c

    Related posts about best-practices