Can function pointers be compared for equality

Discussion in 'C Programming' started by V.Subramanian, India, Jan 23, 2012.

  1. 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
     
    V.Subramanian, India, Jan 23, 2012
    #1
    1. Advertising

  2. On Jan 23, 1:54 pm, "V.Subramanian, India"
    <> 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
     
    Malcolm McLean, Jan 23, 2012
    #2
    1. Advertising

  3. V.Subramanian, India

    Ben Pfaff Guest

    Malcolm McLean <> writes:

    > On Jan 23, 1:54 pm, "V.Subramanian, India"
    > <> 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
     
    Ben Pfaff, Jan 23, 2012
    #3
  4. Malcolm McLean <> writes:

    > On Jan 23, 1:54 pm, "V.Subramanian, India"
    > <> 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.
     
    Ben Bacarisse, Jan 23, 2012
    #4
  5. Malcolm McLean <> writes:

    > <> 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) <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"
     
    Keith Thompson, Jan 23, 2012
    #5
  6. V.Subramanian, India

    Kaz Kylheku Guest

    On 2012-01-23, Ben Pfaff <> wrote:
    > Malcolm McLean <> writes:
    >
    >> On Jan 23, 1:54 pm, "V.Subramanian, India"
    >> <> 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 < > <= >=.


    **** 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".
     
    Kaz Kylheku, Jan 23, 2012
    #6
  7. V.Subramanian, India

    ec429 Guest

    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
     
    ec429, Jan 23, 2012
    #7
  8. V.Subramanian, India

    James Kuyper Guest

    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.
     
    James Kuyper, Jan 23, 2012
    #8
  9. V.Subramanian, India

    Ian Collins Guest

    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
     
    Ian Collins, Jan 23, 2012
    #9
  10. V.Subramanian, India

    Kleuske Guest

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



    --
    Is truth not truth for all?
    -- Natira, "For the World is Hollow and I have Touched
    the Sky", stardate 5476.4.
     
    Kleuske, Jan 23, 2012
    #10
  11. V.Subramanian, India

    James Kuyper Guest

    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.
     
    James Kuyper, Jan 23, 2012
    #11
  12. V.Subramanian, India

    Kaz Kylheku Guest

    On 2012-01-23, 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' ;)


    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".
     
    Kaz Kylheku, Jan 23, 2012
    #12
  13. V.Subramanian, India

    Ian Collins Guest

    On 01/24/12 11:11 AM, Kaz Kylheku wrote:
    > On 2012-01-23, 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' ;)

    >
    > 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
     
    Ian Collins, Jan 23, 2012
    #13
  14. pete <> 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) <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"
     
    Keith Thompson, Jan 24, 2012
    #14
  15. V.Subramanian, India

    Kaz Kylheku Guest

    On 2012-01-24, Keith Thompson <> wrote:
    > pete <> 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.
     
    Kaz Kylheku, Jan 24, 2012
    #15
  16. V.Subramanian, India

    James Kuyper Guest

    On 01/23/2012 10:48 PM, Kaz Kylheku wrote:
    > On 2012-01-24, Keith Thompson <> wrote:
    >> pete <> 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.


    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.
    --
    James Kuyper
     
    James Kuyper, Jan 24, 2012
    #16
  17. V.Subramanian, India

    Kaz Kylheku Guest

    On 2012-01-24, James Kuyper <> wrote:
    > sentence.


    Nope.
     
    Kaz Kylheku, Jan 24, 2012
    #17
  18. On Jan 23, 9: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.


    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?
     
    Nick Keighley, Jan 24, 2012
    #18
  19. yes
     
    Nick Keighley, Jan 24, 2012
    #19
  20. V.Subramanian, India

    Kleuske Guest

    maybe?
     
    Kleuske, Jan 24, 2012
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dwerdna
    Replies:
    0
    Views:
    633
    dwerdna
    Feb 27, 2005
  2. GS
    Replies:
    3
    Views:
    3,118
    shanem
    May 18, 2007
  3. Bengt Richter
    Replies:
    0
    Views:
    294
    Bengt Richter
    Aug 25, 2004
  4. Replies:
    26
    Views:
    791
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    683
Loading...

Share This Page