Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #16054 > unrolled thread

Can function pointers be compared for equality

Started by"V.Subramanian, India" <subramanian100in@yahoo.com>
First post2012-01-23 05:54 -0800
Last post2012-01-24 11:16 +0000
Articles 20 on this page of 24 — 13 participants

Back to article view | Back to comp.lang.c


Contents

  Can function pointers be compared for equality "V.Subramanian, India" <subramanian100in@yahoo.com> - 2012-01-23 05:54 -0800
    Re: Can function pointers be compared for equality Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-01-23 07:00 -0800
      Re: Can function pointers be compared for equality blp@cs.stanford.edu (Ben Pfaff) - 2012-01-23 07:46 -0800
        Re: Can function pointers be compared for equality Kaz Kylheku <kaz@kylheku.com> - 2012-01-23 20:06 +0000
          Re: Can function pointers be compared for equality ec429 <ec429@cam.ac.uk> - 2012-01-23 20:42 +0000
            Re: Can function pointers be compared for equality James Kuyper <jameskuyper@verizon.net> - 2012-01-23 16:04 -0500
              Re: Can function pointers be compared for equality Ian Collins <ian-news@hotmail.com> - 2012-01-24 10:24 +1300
                Re: Can function pointers be compared for equality James Kuyper <jameskuyper@verizon.net> - 2012-01-23 16:48 -0500
                Re: Can function pointers be compared for equality Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-24 03:04 -0800
                  Re: Can function pointers be compared for equality gwowen <gwowen@gmail.com> - 2012-01-24 03:37 -0800
              Re: Can function pointers be compared for equality Kleuske <kleuske@nowhere.net> - 2012-01-23 21:39 +0000
              Re: Can function pointers be compared for equality gwowen <gwowen@gmail.com> - 2012-01-24 03:31 -0800
                Re: Can function pointers be compared for equality ec429 <ec429@cam.ac.uk> - 2012-01-24 13:16 +0000
            Re: Can function pointers be compared for equality Kaz Kylheku <kaz@kylheku.com> - 2012-01-23 22:11 +0000
              Re: Can function pointers be compared for equality Ian Collins <ian-news@hotmail.com> - 2012-01-24 11:34 +1300
      Re: Can function pointers be compared for equality Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-23 16:37 +0000
      Re: Can function pointers be compared for equality Keith Thompson <kst-u@mib.org> - 2012-01-23 09:11 -0800
        Re: Can function pointers be compared for equality pete <pfiland@mindspring.com> - 2012-01-23 20:06 -0500
          Re: Can function pointers be compared for equality Keith Thompson <kst-u@mib.org> - 2012-01-23 17:45 -0800
            Re: Can function pointers be compared for equality Kaz Kylheku <kaz@kylheku.com> - 2012-01-24 03:48 +0000
              Re: Can function pointers be compared for equality James Kuyper <jameskuyper@verizon.net> - 2012-01-23 22:59 -0500
                Re: Can function pointers be compared for equality Kaz Kylheku <kaz@kylheku.com> - 2012-01-24 04:22 +0000
                  Re: Can function pointers be compared for equality Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-24 03:07 -0800
                    Re: Can function pointers be compared for equality Kleuske <kleuske@nowhere.net> - 2012-01-24 11:16 +0000

Page 1 of 2  [1] 2  Next page →


#16054 — Can function pointers be compared for equality

From"V.Subramanian, India" <subramanian100in@yahoo.com>
Date2012-01-23 05:54 -0800
SubjectCan function pointers be compared for equality
Message-ID<16c3fab7-bcd4-40dd-8208-93e7358dec1e@kn4g2000pbc.googlegroups.com>
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

[toc] | [next] | [standalone]


#16055

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2012-01-23 07:00 -0800
Message-ID<ad3ff3b8-b520-40f0-8e73-4a66a1426e37@n6g2000vbz.googlegroups.com>
In reply to#16054
On Jan 23, 1:54 pm, "V.Subramanian, India"
<subramanian10...@yahoo.com> wrote:
>
> 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.
--
MiniBasic - how to write a script interpreter
http://www.malcommclean.site11.com/www

[toc] | [prev] | [next] | [standalone]


#16056

Fromblp@cs.stanford.edu (Ben Pfaff)
Date2012-01-23 07:46 -0800
Message-ID<8739b6zajt.fsf@blp.benpfaff.org>
In reply to#16055
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:

> On Jan 23, 1:54 pm, "V.Subramanian, India"
> <subramanian10...@yahoo.com> wrote:
>>
>> 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 < > <= >=.
-- 
"This is a wonderful answer.
 It's off-topic, it's incorrect, and it doesn't answer the question."
--Richard Heathfield

[toc] | [prev] | [next] | [standalone]


#16069

FromKaz Kylheku <kaz@kylheku.com>
Date2012-01-23 20:06 +0000
Message-ID<20120123115909.372@kylheku.com>
In reply to#16056
On 2012-01-23, Ben Pfaff <blp@cs.stanford.edu> wrote:
> Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
>
>> On Jan 23, 1:54 pm, "V.Subramanian, India"
>> <subramanian10...@yahoo.com> wrote:
>>>
>>> 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 < > <= >=.

Fuck 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".

[toc] | [prev] | [next] | [standalone]


#16071

Fromec429 <ec429@cam.ac.uk>
Date2012-01-23 20:42 +0000
Message-ID<jfkgo9$dl2$1@dont-email.me>
In reply to#16069
On 23/01/12 20:06, Kaz Kylheku wrote:
> 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
-- 
'sane', adj.: see 'unimaginative'
   on the web - http://jttlov.no-ip.org

[toc] | [prev] | [next] | [standalone]


#16072

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-01-23 16:04 -0500
Message-ID<4F1DCB5E.7080102@verizon.net>
In reply to#16071
On 01/23/2012 03:42 PM, ec429 wrote:
> On 23/01/12 20:06, Kaz Kylheku wrote:
>> 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' ;)

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

[toc] | [prev] | [next] | [standalone]


#16073

FromIan Collins <ian-news@hotmail.com>
Date2012-01-24 10:24 +1300
Message-ID<9o61fhFbb0U2@mid.individual.net>
In reply to#16072
On 01/24/12 10:04 AM, James Kuyper wrote:
> On 01/23/2012 03:42 PM, ec429 wrote:
>> On 23/01/12 20:06, Kaz Kylheku wrote:
>>> 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' ;)
>
> 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.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#16076

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-01-23 16:48 -0500
Message-ID<4F1DD5B5.9040308@verizon.net>
In reply to#16073
On 01/23/2012 04:24 PM, Ian Collins wrote:
> On 01/24/12 10:04 AM, James Kuyper wrote:
>> On 01/23/2012 03:42 PM, ec429 wrote:
>>> On 23/01/12 20:06, Kaz Kylheku wrote:
>>>> 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' ;)
>>
>> 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.

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.

[toc] | [prev] | [next] | [standalone]


#16090

FromNick Keighley <nick_keighley_nospam@hotmail.com>
Date2012-01-24 03:04 -0800
Message-ID<9fd251f5-7984-4d2e-9212-b3115f991ea9@j15g2000yqb.googlegroups.com>
In reply to#16073
On Jan 23, 9:24 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 01/24/12 10:04 AM, James Kuyper wrote:
> > On 01/23/2012 03:42 PM, ec429 wrote:
> >> On 23/01/12 20:06, Kaz Kylheku wrote:

> >>> 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' ;)
>
> > 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.

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?

[toc] | [prev] | [next] | [standalone]


#16094

Fromgwowen <gwowen@gmail.com>
Date2012-01-24 03:37 -0800
Message-ID<b92f09fe-1450-4e5c-a018-d671e0e82e84@b20g2000yqb.googlegroups.com>
In reply to#16090
On Jan 24, 11:04 am, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
> They also think because I'm a computer programmer I should be skilled at
> mental arithmatic.

... or at least able to spell it :)

> Do JCB (Back Hoe) drivers dig ditches?

Yeah, they think they're pretty good.... But seriously, OED-approved-
generic-or-otherwise, JCB remains a trademark of "JC Bamford
Excavators" and its various worldwide companies.  What they make
(predominately) are backhoes and excavators.

[toc] | [prev] | [next] | [standalone]


#16074

FromKleuske <kleuske@nowhere.net>
Date2012-01-23 21:39 +0000
Message-ID<jfkk20$3vq$1@dont-email.me>
In reply to#16072
On Mon, 23 Jan 2012 16:04:30 -0500, James Kuyper saw fit to publish the
following:

> On 01/23/2012 03:42 PM, ec429 wrote:
>> On 23/01/12 20:06, Kaz Kylheku wrote:
>>> 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' ;)
> 
> That's true in British English. American English uses "math". I'm not
> sure what the other dialects use.

To be frank: I don't give a flying fuck.



-- 
Is truth not truth for all?
		-- Natira, "For the World is Hollow and I have Touched
		   the Sky", stardate 5476.4.

[toc] | [prev] | [next] | [standalone]


#16093

Fromgwowen <gwowen@gmail.com>
Date2012-01-24 03:31 -0800
Message-ID<fa2e7dcd-9285-4269-8c0a-656302c28c70@dp8g2000vbb.googlegroups.com>
In reply to#16072
On Jan 23, 9:04 pm, James Kuyper <jameskuy...@verizon.net> wrote:
> On 01/23/2012 03:42 PM, ec429 wrote:
>
> > On 23/01/12 20:06, Kaz Kylheku wrote:
> >> 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' ;)
>
> That's true in British English. American English uses "math". I'm not
> sure what the other dialects use.

Math ith a Roman Catholic servith.

[toc] | [prev] | [next] | [standalone]


#16096

Fromec429 <ec429@cam.ac.uk>
Date2012-01-24 13:16 +0000
Message-ID<jfmavn$tm6$1@dont-email.me>
In reply to#16093
On 24/01/12 11:31, gwowen wrote:
> On Jan 23, 9:04 pm, James Kuyper<jameskuy...@verizon.net>  wrote:
>> On 01/23/2012 03:42 PM, ec429 wrote:
>>
>>> On 23/01/12 20:06, Kaz Kylheku wrote:
>>>> 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' ;)
>>
>> That's true in British English. American English uses "math". I'm not
>> sure what the other dialects use.
>
> Math ith a Roman Catholic servith.
But this is comp.lang.c, not comp.lang.lithp!
-e
-- 
'sane', adj.: see 'unimaginative'
   on the web - http://jttlov.no-ip.org

[toc] | [prev] | [next] | [standalone]


#16077

FromKaz Kylheku <kaz@kylheku.com>
Date2012-01-23 22:11 +0000
Message-ID<20120123141036.779@kylheku.com>
In reply to#16071
On 2012-01-23, ec429 <ec429@cam.ac.uk> wrote:
> On 23/01/12 20:06, Kaz Kylheku wrote:
>> 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' ;)

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

[toc] | [prev] | [next] | [standalone]


#16079

FromIan Collins <ian-news@hotmail.com>
Date2012-01-24 11:34 +1300
Message-ID<9o65jiFbb0U3@mid.individual.net>
In reply to#16077
On 01/24/12 11:11 AM, Kaz Kylheku wrote:
> On 2012-01-23, ec429<ec429@cam.ac.uk>  wrote:
>> On 23/01/12 20:06, Kaz Kylheku wrote:
>>> 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' ;)
>
> 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".

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#16059

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2012-01-23 16:37 +0000
Message-ID<0.61b3f28cc5ca78b32374.20120123163725GMT.87ipk2iddm.fsf@bsb.me.uk>
In reply to#16055
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:

> On Jan 23, 1:54 pm, "V.Subramanian, India"
> <subramanian10...@yahoo.com> wrote:
>>
>> Is this equality comparison check of function pointers, legal, correct
>> and allowed ?
<snip>
> 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".

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#16063

FromKeith Thompson <kst-u@mib.org>
Date2012-01-23 09:11 -0800
Message-ID<ln8vkywdi3.fsf@nuthaus.mib.org>
In reply to#16055
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:

> <subramanian10...@yahoo.com> wrote:
>>
>> 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.

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.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#16084

Frompete <pfiland@mindspring.com>
Date2012-01-23 20:06 -0500
Message-ID<4F1E03FE.B21@mindspring.com>
In reply to#16063
Keith Thompson wrote:

>  ~= 

ITYM !=

-- 
pete

[toc] | [prev] | [next] | [standalone]


#16086

FromKeith Thompson <kst-u@mib.org>
Date2012-01-23 17:45 -0800
Message-ID<lnhazl7u10.fsf@nuthaus.mib.org>
In reply to#16084
pete <pfiland@mindspring.com> writes:
> Keith Thompson wrote:
>>  ~= 
>
> ITYM !=

Yes, thanks.

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

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#16087

FromKaz Kylheku <kaz@kylheku.com>
Date2012-01-24 03:48 +0000
Message-ID<20120123194642.176@kylheku.com>
In reply to#16086
On 2012-01-24, Keith Thompson <kst-u@mib.org> wrote:
> pete <pfiland@mindspring.com> writes:
>> Keith Thompson wrote:
>>>  ~= 
>>
>> ITYM !=
>
> 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.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c


csiph-web