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?

--
-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

--
| 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

 Page 1 of 1 [ 4 post ]

Relevant Pages