Validity of simplified notation for calling functions from function pointers 
Author Message
 Validity of simplified notation for calling functions from function pointers

Suppose we have a function named example that takes as one of its
arguments a pointer to a function of two ints returning an int.

void example(int (*comp)(int, int), /* ...other args... */);

As long as I've been programming in C, whenever within the body of
example I want to call the comparison function pointed to by comp I
would (for example) do:

z = (*comp)(x, y);

In particular, this is the way it is always done in K&R2. However,
I've lately been seeing code that does this just as:

z = comp(x, y);

I've tried writing my own code this way and my compiler gives me no
warnings and it always works perfectly, so I presume it is a perfectly
valid "abreviation". But I want to know:

1. Is this simplified notation in fact perfectly valid?
2. Why doesn't K&R2 ever seem to use this simplified notation?



Sat, 01 May 2004 15:17:21 GMT  
 Validity of simplified notation for calling functions from function pointers


Quote:
> void example(int (*comp)(int, int), /* ...other args... */);
> z = (*comp)(x, y);
> z = comp(x, y);

> 1. Is this simplified notation in fact perfectly valid?

Yes. It's defined in ISO-C 90

Quote:
> 2. Why doesn't K&R2 ever seem to use this simplified notation?

Ask the authors!


--
-hs- emdel at noos.fr "support Afghans against Taleban"
"Car les bandits qui sont cause des guerres
 N'en meurent jamais, on n'tue qu'les innocents."
Gaston Monthus -- La Butte Rouge



Sat, 01 May 2004 15:33:28 GMT  
 Validity of simplified notation for calling functions from function pointers

=>Suppose we have a function named example that takes as one of its
=>arguments a pointer to a function of two ints returning an int.
=>
=>void example(int (*comp)(int, int), /* ...other args... */);
=>
=>As long as I've been programming in C, whenever within the body of
=>example I want to call the comparison function pointed to by comp I
=>would (for example) do:
=>
=>z = (*comp)(x, y);
=>
=>In particular, this is the way it is always done in K&R2. However,
=>I've lately been seeing code that does this just as:
=>
=>z = comp(x, y);
=>
=>I've tried writing my own code this way and my compiler gives me no
=>warnings and it always works perfectly, so I presume it is a perfectly
=>valid "abreviation". But I want to know:
=>
=>1. Is this simplified notation in fact perfectly valid?
=>2. Why doesn't K&R2 ever seem to use this simplified notation?

I think that one reason could definitely be clarity in reviewing code
rapidly... If you see the (*name) construct, you're more apt to realize
it is a dynamic function pointer and not a truly defined function name.

You can always imagine an inept programmer having to later maintain your
source and spend a while searching for 'comp' in the function names..
Sure, most programmers would notice, but, some would take a kick to the
head to realize it.

--
| Stephen "Slepp" Olesen / VE6SLP
| Edmonton, Alberta, Canada / (780) 425-4798
| President of Geeks Anonymous + http://www.geeksanon.ca/
+---------------------------------



Sun, 02 May 2004 16:26:21 GMT  
 Validity of simplified notation for calling functions from function pointers
In fact, it doesn't matter how many *'s are in front of a
pointer-to-function, you end up with a pointer to the function.

--
#include <standard.disclaimer>
 _
Kevin D Quitt  USA 91351-4454           96.37% of all statistics are made up
Per the FCA, this email address may not be added to any commercial mail list



Mon, 03 May 2004 03:10:39 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to use (pointer to function), and function and pointer to (pointer to function)

2. Function to simplify writing malloc( ) calls

3. Declaration of functions called via function pointers

4. Pointer Functions and Pointers to Pointer Functions

5. Call a function in a function with name of a function as parameter

6. Condition breakpoints and function calls and function calls in Immediate window

7. Question about signal()/pointers to functions that return pointers to functions

8. function pointers and function pointers array

9. C++ function pointers versus C function pointer problem

10. Pointer to function returning pointer to function returning...

11. simplify logical functions

12. function pointers, casting, call stack overwriting

 

 
Powered by phpBB® Forum Software