Can function pointers be compared for equality

  • Thread starter V.Subramanian, India
  • Start date
V

V.Subramanian, India

I am using Intel Pentium D processor-based RedHat operating system
with gcc version 3.4.3
For a file named x.c, I compile it with the following
command:
gcc -std=c99 -pedantic -Wall -Wextra x.c

Consider the following functions:

inline int cmpfnForAscendingOrder(
KeyType firstKey,
KeyType secondKey)
{
int ret;

if (firstKey < secondKey)
ret = -1;
else if (firstKey == secondKey)
ret = 0;
else
ret = 1;

return ret;
}

inline int cmpfnForDescendingOrder(
KeyType firstKey,
KeyType secondKey)
{
return -cmpfnForAscendingOrder(
firstKey,
secondKey);
}

bool deleteLastKNodesWithKeyInSortedList(
NoOfNodes k,
KeyType key,
int (*compareKey)(KeyType firstKey,
KeyType secondKey),
ListNode **node)
{
// ...

int (*tempCompareKey)(
KeyType firstKey,
KeyType secondKey);

// Is the following allowed ?
// Note that it works in my implementation.
tempCompareKey =
(compareKey ==
&cmpfnForAscendingOrder)
? &cmpfnForDescendingOrder
: &cmpfnForAscendingOrder;

// ...
}

I invoke the function deleteLastKNodesWithKeyInSortedList() with
appropriate arguments. One of them is a function pointer which is the
third parameter. For this third parameter, I pass either
'cmpfnForAscendingOrder' or
'cmpfnForDescendingOrder'.

Now inside deleteLastKNodesWithKeyInSortedList(),
I need to know whether the third parameter 'compareKey' holds
'cmpfnForAscendingOrder' or
'cmpfnForDescendingOrder' and assign one of these two function
pointers(in fact revert) to the variable 'tempCompareKey'. For this, I
do an equality comparison check as follows:
(compareKey == &cmpfnForAscendingOrder)
Is this equality comparison check of function pointers, legal, correct
and allowed ? (Note that it works in my implementation of gcc. But I
do not want to go by one particular implementation).

Please explain.

Thanks
V.Subramanian
India
 
M

Malcolm McLean

Is this equality comparison check of function pointers, legal, correct
and allowed ? (Note that it works in my implementation of gcc. But I
do not want to go by one particular implementation).

Please explain.
You can compare function ponters for equality. However you can't
compare for inequality. The > and < operators only refer to pointers
which are within the same object, and two fucntion pointers cannot be
within the same object, except for the special case of identity.
However on most platforms code addresses can be sorted into ascending
or descending order, and comparison for inequality will work as you
would expect. Also, you can convert the pointer to a raw stream of
bytes, and comparision must work.
 
B

Ben Pfaff

Malcolm McLean said:
Is this equality comparison check of function pointers, legal, correct
and allowed ? (Note that it works in my implementation of gcc. But I
do not want to go by one particular implementation).

Please explain.
You can compare function ponters for equality. However you can't
compare for inequality. [...]

In C, != is the inequality operator. You can compare function
pointers with !=. You cannot compare them with the relational
operators < > <= >=.
 
B

Ben Bacarisse

You can compare function ponters for equality. However you can't
compare for inequality. The > and < operators only refer to pointers
which are within the same object, and two fucntion pointers cannot be
within the same object, except for the special case of identity.
However on most platforms code addresses can be sorted into ascending
or descending order, and comparison for inequality will work as you
would expect. Also, you can convert the pointer to a raw stream of
bytes, and comparision must work.

That last remark is not really true. If the pointers use some
representation that is not unique, comparing the representations won't
even give you a reliable equality operation. What's worse is that
result of any comparison won't necessarily be stable: it might change at
any time if, for example, the stored pointer gets "normalised".
 
K

Keith Thompson

Malcolm McLean said:
You can compare function ponters for equality. However you can't
compare for inequality. The > and < operators only refer to pointers
which are within the same object, and two fucntion pointers cannot be
within the same object, except for the special case of identity.

Function pointers never point to *any* object. The relational
operators (< <= > >=) are not defined for function pointers because
the standard doesn't define them. Two function pointers are equal,
as determined by the = and ~= operator, if and only if they're both
null pointers or they both point to the same function.
However on most platforms code addresses can be sorted into ascending
or descending order, and comparison for inequality will work as you
would expect. Also, you can convert the pointer to a raw stream of
bytes, and comparision must work.

x < y, where x and y are function pointers, is a constraint
violation; a conforming compiler must issue a diagnostic, and
may reject the translation unit. Comparing the raw bytes is not
guaranteed to be meaningful.
 
K

Kaz Kylheku

Malcolm McLean said:
Is this equality comparison check of function pointers, legal, correct
and allowed ? (Note that it works in my implementation of gcc. But I
do not want to go by one particular implementation).

Please explain.
You can compare function ponters for equality. However you can't
compare for inequality. [...]

In C, != is the inequality operator. You can compare function
pointers with !=. You cannot compare them with the relational
operators < > <= >=.

**** the C terminlogy and the insipid monkeys behind it, who long should have
called it quits.

In English math terminology, this is called an equation:

3x + 4 = 7

and this is called an inequality:

3x + 4 < 7

(Those of you who don't remember, ask the nearest fifth grader.)

A "relation" is a mapping between sets. Equality is a kind of relation
("equivalence relation") and so the == operator can rightfully be called
"relational".
 
E

ec429

In English math terminology
<snip gibberish about equations>
Ok, maybe we should call them the "order" or "poset" operators?
Also, in _English_, math is called 'maths' ;)
-e
 
J

James Kuyper

<snip gibberish about equations>
Ok, maybe we should call them the "order" or "poset" operators?
Also, in _English_, math is called 'maths' ;)

That's true in British English. American English uses "math". I'm not
sure what the other dialects use.
 
I

Ian Collins

That's true in British English. American English uses "math". I'm not
sure what the other dialects use.

In all those I'm aware of, "maths". Although a lot of what I see
referred to as "math" is just basic arithmetic.
 
J

James Kuyper

In all those I'm aware of, "maths". Although a lot of what I see
referred to as "math" is just basic arithmetic.

For me, most of what I heard called "maths" was at quite a bit higher
level. :) When I spent a year studying at Cambridge University, what I
earned by passing the Mathematical Tripos was called a "Certificate of
Advanced Study in Applied Mathematics". When I applied to various
American grad schools, and later when I was applying for jobs, I always
had to explain what it meant. In the US, "Applied Mathematics" is not
normally understood to describe such things as general relativity and
relativistic quantum field theory. Also, Certificates are not normally
given out for graduate level work.
 
K

Kaz Kylheku

<snip gibberish about equations>
Ok, maybe we should call them the "order" or "poset" operators?
Also, in _English_, math is called 'maths' ;)

Your ISP clearly blocks non-British web pages, so that you're not shocked by
obscenities like "math co-processor". Or even, heaven forbid,
"math coprocessor".
 
I

Ian Collins

Your ISP clearly blocks non-British web pages, so that you're not shocked by
obscenities like "math co-processor". Or even, heaven forbid,
"math coprocessor".

Intel got it right: their documentation uses the term "floating-point
arithmetic".
 
K

Kaz Kylheku

Yes, thanks.

Your correction would have been even more helpful if you'd quoted some
context.

Kids, it should be obvious that the context is the C programming language which
has no ~= operator. The irony.
 
J

James Kuyper

Kids, it should be obvious that the context is the C programming language which
has no ~= operator. The irony.

No, the context was not just the C programming language in general - if
it had, Keith's comment would not have been justified. The context was a
particular sentence in a particular paragraph in a particular message
posted by Keith. The correction didn't need to quote the entire message,
but it should at least have quoted that particular sentence.
 
N

Nick Keighley

In all those I'm aware of, "maths".  Although a lot of what I see
referred to as "math" is just basic arithmetic.

people confuse the two, people belive because I have some knowledge of
mathematics that I should be able to add up. They also think because
I'm a computer programmer I should be skilled at mental arithmatic.
Hell, I *became* a computer programmer so I wouldn't have to do that
stuff! Do JCB (Back Hoe) drivers dig ditches?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top