typecast function pointer

Discussion in 'C Programming' started by Mark, Jan 28, 2012.

  1. Mark

    Mark Guest

    Hello

    typedef signed int s_int32_t;

    typedef void (*del_cb_t) (void *val);
    typedef s_int32_t (*cmp_cb_t) (void *val1, void *val2);

    int obj_create (cmp_cb_t cmp, del_cb_t del);

    int obj_create (cmp_cb_t cmp, del_cb_t del)
    {
    /* do the stuff */
    }

    ....
    int my_cmp (void * data1, void * data2)
    {
    }

    ret = obj_create ((cmp_cb_t)my_cmp, (del_cb_t)NULL);

    Does it have any useful effect to typecast 'my_cmp' given that return value
    is different with what defines typedef ?


    Mark
    Mark, Jan 28, 2012
    #1
    1. Advertising

  2. Mark

    Eric Sosman Guest

    On 1/28/2012 8:15 AM, Mark wrote:
    > Hello
    >
    > typedef signed int s_int32_t;
    >
    > typedef void (*del_cb_t) (void *val);
    > typedef s_int32_t (*cmp_cb_t) (void *val1, void *val2);
    >
    > int obj_create (cmp_cb_t cmp, del_cb_t del);
    >
    > int obj_create (cmp_cb_t cmp, del_cb_t del)
    > {
    > /* do the stuff */
    > }
    >
    > ...
    > int my_cmp (void * data1, void * data2)
    > {
    > }
    >
    > ret = obj_create ((cmp_cb_t)my_cmp, (del_cb_t)NULL);
    >
    > Does it have any useful effect to typecast 'my_cmp' given that return value
    > is different with what defines typedef ?


    No, because `int' and `s_int32_t' are *not* different types.
    A typedef declaration does not create a new type; it just creates
    an additional name for an existing type. Also, `signed int' and
    `int' are not different types, but just two different ways to
    name the same type. All of `s_int32_t', `signed int', and `int'
    are different ways of naming one type, and all three aliases can
    be used interchangeably.[*],[**]

    If my_cmp() were in fact different -- if it returned a `float',
    say, or an `unsigned long' -- then the cast would be both necessary
    an *in*sufficient. "Necessary" in the sense that the compiler would
    be required to issue a diagnostic if the cast were absent, but
    "insufficient" in that even if it quieted the diagnostic the program
    would still be wrong, with a wrongness that might not be detected
    until run-time.

    The other cast is also unnecessary. Because obj_create() is
    declared with a prototype, the compiler "knows" the types of its
    parameters and will convert the `NULL' argument to the proper
    type.[***]

    [*] Although `int' and `signed int' name the same type, note
    that `char' and `signed char' do not. For historical reasons,
    `char' is somewhat special, and is distinct from both `signed char'
    and `unsigned char' even though it behaves just like one or the
    other of them.

    [**] In a bit-field declaration (like `int x : 4;' in a struct
    or union), `int' and `signed int' may mean different things. Again
    for historical reasons, a plain `int' bit-field may be either signed
    or unsigned at the compiler's discretion, while `signed int x : 4;'
    is definitely signed. It's helpful to adopt the viewpoint that
    although `int' and `signed int' are the same, `int:N' and
    `signed int:N' are distinct.

    [***] Note that the compiler has no information about the types
    of variadic parameters, those that match the `...' portion of a
    variable-argument prototype. Casts are sometimes needed when
    passing arguments to match those parameters; a cast is *always*
    needed when passing `NULL' arguments to such parameters.

    --
    Eric Sosman
    d
    Eric Sosman, Jan 28, 2012
    #2
    1. Advertising

  3. "Mark" <> writes:

    > typedef signed int s_int32_t;
    >
    > typedef void (*del_cb_t) (void *val);
    > typedef s_int32_t (*cmp_cb_t) (void *val1, void *val2);


    It would be better is these could be const void * parameters.

    > int obj_create (cmp_cb_t cmp, del_cb_t del);
    >
    > int obj_create (cmp_cb_t cmp, del_cb_t del)
    > {
    > /* do the stuff */
    > }
    >
    > ...
    > int my_cmp (void * data1, void * data2)
    > {
    > }
    >
    > ret = obj_create ((cmp_cb_t)my_cmp, (del_cb_t)NULL);
    >
    > Does it have any useful effect to typecast 'my_cmp' given that return value
    > is different with what defines typedef ?


    Not really. I suppose it just possible that it might suppress a
    warning. No diagnostic is required, but I can maybe an overly "helpful"
    compiler might warn about the code without the cast.

    However, you are setting yourself up for a maintenance problem later on.
    If, in some future port of the software, s_int32_t ends not being
    compatible with int, the code will be undefined and the cast might mask
    all warnings about that. Is not possible to define my_cmp as returning
    an s_int32_t?

    --
    Ben.
    Ben Bacarisse, Jan 28, 2012
    #3
  4. Mark

    Mark Guest

    "Eric Sosman" <> wrote in message
    news:jg0ui5$o9l$...
    > No, because `int' and `s_int32_t' are *not* different types.
    > A typedef declaration does not create a new type; it just creates
    > an additional name for an existing type. Also, `signed int' and
    > `int' are not different types, but just two different ways to
    > name the same type. All of `s_int32_t', `signed int', and `int'
    > are different ways of naming one type, and all three aliases can
    > be used interchangeably.[*],[**]


    Thanks for comprehensive answers!

    What can be the reason to declare a function returning 'signed int' instead
    of 'int' if those are all the same types? IMHO it only brings in confusion,
    as in my case.

    Mark.
    Mark, Jan 29, 2012
    #4
  5. Mark

    Ian Collins Guest

    On 01/30/12 10:47 AM, Mark wrote:
    > "Eric Sosman"<> wrote in message
    > news:jg0ui5$o9l$...
    >> No, because `int' and `s_int32_t' are *not* different types.
    >> A typedef declaration does not create a new type; it just creates
    >> an additional name for an existing type. Also, `signed int' and
    >> `int' are not different types, but just two different ways to
    >> name the same type. All of `s_int32_t', `signed int', and `int'
    >> are different ways of naming one type, and all three aliases can
    >> be used interchangeably.[*],[**]

    >
    > Thanks for comprehensive answers!
    >
    > What can be the reason to declare a function returning 'signed int' instead
    > of 'int' if those are all the same types? IMHO it only brings in confusion,
    > as in my case.


    There aren't any really. Maybe the original authour was trying to look
    smart?

    --
    Ian Collins
    Ian Collins, Jan 29, 2012
    #5
  6. "Mark" <> writes:
    > "Eric Sosman" <> wrote in message
    > news:jg0ui5$o9l$...
    >> No, because `int' and `s_int32_t' are *not* different types.
    >> A typedef declaration does not create a new type; it just creates
    >> an additional name for an existing type. Also, `signed int' and
    >> `int' are not different types, but just two different ways to
    >> name the same type. All of `s_int32_t', `signed int', and `int'
    >> are different ways of naming one type, and all three aliases can
    >> be used interchangeably.[*],[**]

    >
    > Thanks for comprehensive answers!
    >
    > What can be the reason to declare a function returning 'signed int' instead
    > of 'int' if those are all the same types? IMHO it only brings in confusion,
    > as in my case.


    The compiler doesn't care whether you write "int", or "signed int", or
    "signed", or "int unsigned" for that matter.

    You might write "signed int" for emphasis, to distinguish it from
    "unsigned int":

    unsigned int this_function(void);
    signed int that_function(void);

    (And "signed int" is not necessarily "int" when used to declare a bit
    field, but the question was about function return types, so that doesn't
    apply.)

    --
    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 29, 2012
    #6
  7. Mark

    Eric Sosman Guest

    On 1/29/2012 4:47 PM, Mark wrote:
    > "Eric Sosman"<> wrote in message
    > news:jg0ui5$o9l$...
    >> No, because `int' and `s_int32_t' are *not* different types.
    >> A typedef declaration does not create a new type; it just creates
    >> an additional name for an existing type. Also, `signed int' and
    >> `int' are not different types, but just two different ways to
    >> name the same type. All of `s_int32_t', `signed int', and `int'
    >> are different ways of naming one type, and all three aliases can
    >> be used interchangeably.[*],[**]

    >
    > Thanks for comprehensive answers!
    >
    > What can be the reason to declare a function returning 'signed int' instead
    > of 'int' if those are all the same types? IMHO it only brings in confusion,
    > as in my case.


    The `signed' keyword was absent from original prehistoric C,
    and was added in the first ("C89", "ANSI") version of the Standard.
    The intent was to allow the programmer to write `signed char' or
    `unsigned char' when a tiny signed or unsigned integer was desired,
    or to write plain `char' when the signedness was unimportant and
    the choice could be left to the compiler.

    When `signed char' became available, the committee decided that
    `signed int' and `signed short' and `signed long' should also be
    allowed, even though `signed' added no new meaning (these integer
    flavors were and still are always signed). Perhaps the committee was
    inspired by a desire for "regularity" in the language, or perhaps
    they were thinking of declarations constructed by macro expansion.
    Whatever their reason, `signed int' is now an alias for `int'.

    To me, it seems pointless to write `signed int' instead of `int'.
    Some might feel that `signed int' is more self-documenting in that
    it emphasizes the fact that the type holds signed values, but it seems
    to me that this is such a basic property of C that anyone who needs
    the emphasis probably shouldn't be using the language. (I also find
    `short int' similarly useless, and avoid `long int signed long' as
    a threat to sanity.) Tastes vary, though, and a C programmer must be
    prepared for the occasional "unusual" declaration.

    --
    Eric Sosman
    d
    Eric Sosman, Jan 29, 2012
    #7
  8. Mark

    Ike Naar Guest

    On 2012-01-29, Keith Thompson <> wrote:
    > The compiler doesn't care whether you write "int", or "signed int", or
    > "signed", or "int unsigned" for that matter.


    That sentence (or else the compiler) needs to be fixed.
    Ike Naar, Jan 30, 2012
    #8
  9. Ike Naar <> writes:
    > On 2012-01-29, Keith Thompson <> wrote:
    >> The compiler doesn't care whether you write "int", or "signed int", or
    >> "signed", or "int unsigned" for that matter.

    >
    > That sentence (or else the compiler) needs to be fixed.


    Oops, the last should be "int signed".

    To summarize, the following are all equivalent names for the same type:

    int
    signed int
    int signed
    signed

    The "int signed" form is rarely used, but the standard permits type
    specifiers to appear in any order.

    --
    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 30, 2012
    #9
  10. Eric Sosman <> writes:
    [...]
    > To me, it seems pointless to write `signed int' instead of `int'.

    [...]

    Except for bit fields, of course.

    --
    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 30, 2012
    #10
    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. Christian Wittrock

    16 bit pointer typecast on 16 bit system

    Christian Wittrock, Jul 3, 2006, in forum: C Programming
    Replies:
    17
    Views:
    805
  2. Ulrich Hobelmann

    Typecast illegal from int to pointer?

    Ulrich Hobelmann, Jun 4, 2006, in forum: C++
    Replies:
    2
    Views:
    342
    Ulrich Hobelmann
    Jun 4, 2006
  3. icedac
    Replies:
    4
    Views:
    721
    James Kanze
    Mar 5, 2008
  4. su
    Replies:
    3
    Views:
    770
    CBFalconer
    Jan 23, 2009
  5. dost
    Replies:
    1
    Views:
    522
    Michael DOUBEZ
    Jun 15, 2011
Loading...

Share This Page