typedef and #define

Discussion in 'C Programming' started by vivek, Mar 4, 2008.

  1. vivek

    vivek Guest

    What will happen if i replace a typedef with a #define?
     
    vivek, Mar 4, 2008
    #1
    1. Advertising

  2. vivek

    santosh Guest

    vivek wrote:

    > What will happen if i replace a typedef with a #define?


    See:

    <http://c-faq.com/decl/typedefvsdefine.html>
     
    santosh, Mar 4, 2008
    #2
    1. Advertising

  3. vivek

    Chris Dollin Guest

    vivek wrote:

    > What will happen if i replace a typedef with a #define?


    Your code will break [1].

    [1] Probably. Why court disaster? Why replace the right construct with
    the wrong one?

    --
    "Ashes are burning the way." - Renaissance, /Ashes Are Burning/

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
     
    Chris Dollin, Mar 4, 2008
    #3
  4. vivek

    John Bode Guest

    On Mar 4, 4:56 am, vivek <> wrote:
    > What will happen if i replace a typedef with a #define?


    It depends. Let's suppose you've created a typedef for a pointer to
    int:

    typedef int *iptr;
    iptr px, py;

    Both px and py will be typed int*.

    Now we replace it with a macro:
    #define IPTR int *
    IPTR px, py;

    After preprocessing, this will expand to
    int * px, py;

    In this case, only px will be typed int*; py will be a plain int.

    So, for this particular case, you get different results.

    Some typedefs would be difficult to replace with a macro. For
    example, take

    typedef int *(*(*foo)[20])(char c);

    foo is a synonym for "pointer to 20-element array of pointers to
    functions taking a char parameter and returning a pointer to int." A
    simple macro to replace this typedef would be, well, not so simple.
     
    John Bode, Mar 4, 2008
    #4
  5. vivek

    Morris Dovey Guest

    John Bode wrote:
    >
    > On Mar 4, 11:44 am, Kaz Kylheku <> wrote:
    > > On Mar 4, 2:56 am, vivek <> wrote:
    > >
    > > > What will happen if i replace a typedef with a #define?

    > >
    > > Nothing of consequence.
    > >
    > > Eminent computer scientist S**** N**** demonstrated, years ago, that
    > > they are the same thing.
    > >
    > > :)

    >
    > DO NOT SPEAK THAT NAME, lest he notice and come back to pester us
    > again, with the Three Stooges (Twink, McCormack, and Just Richard)
    > egging him on.


    Heh - might as well invoke all the demons...

    What ever happened to that lad who preached that hexadecimal was
    the one true number system?

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto
     
    Morris Dovey, Mar 4, 2008
    #5
  6. vivek

    Kaz Kylheku Guest

    On Mar 4, 2:56 am, vivek <> wrote:
    > What will happen if i replace a typedef with a #define?


    Nothing of consequence.

    Eminent computer scientist Scott Nudds demonstrated, years ago, that
    they are the same thing.

    :)
     
    Kaz Kylheku, Mar 4, 2008
    #6
  7. vivek

    John Bode Guest

    On Mar 4, 11:44 am, Kaz Kylheku <> wrote:
    > On Mar 4, 2:56 am, vivek <> wrote:
    >
    > > What will happen if i replace a typedef with a #define?

    >
    > Nothing of consequence.
    >
    > Eminent computer scientist S**** N**** demonstrated, years ago, that
    > they are the same thing.
    >
    > :)


    DO NOT SPEAK THAT NAME, lest he notice and come back to pester us
    again, with the Three Stooges (Twink, McCormack, and Just Richard)
    egging him on.
     
    John Bode, Mar 4, 2008
    #7
  8. vivek

    Bartc Guest

    "John Bode" <> wrote in message
    news:...
    > On Mar 4, 4:56 am, vivek <> wrote:
    >> What will happen if i replace a typedef with a #define?


    > Some typedefs would be difficult to replace with a macro. For
    > example, take
    >
    > typedef int *(*(*foo)[20])(char c);


    >foo is a synonym for "pointer to 20-element array of pointers to
    >functions taking a char parameter and returning a pointer to int."


    Are you saying that:

    typedef int *(*(*foo)[20])(char c);

    Creates a complex type with an alias of foo; while:

    int *(*(*bar)[20])(char c);

    Creates a variable bar with that same type? (Not sure what the 'c' is doing
    in there).

    How would I specify that same complex type without either the foo or bar
    tag, as in a cast for example? Is it just:

    (int *(*(*)[20])(char c))

    ?

    --
    Bart
     
    Bartc, Mar 4, 2008
    #8
  9. vivek

    John Bode Guest

    On Mar 4, 12:23 pm, "Bartc" <> wrote:
    > "John Bode" <> wrote in message
    >
    > news:...
    >
    > > On Mar 4, 4:56 am, vivek <> wrote:
    > >> What will happen if i replace a typedef with a #define?

    > > Some typedefs would be difficult to replace with a macro. For
    > > example, take

    >
    > > typedef int *(*(*foo)[20])(char c);
    > >foo is a synonym for "pointer to 20-element array of pointers to
    > >functions taking a char parameter and returning a pointer to int."

    >
    > Are you saying that:
    >
    > typedef int *(*(*foo)[20])(char c);
    >
    > Creates a complex type with an alias of foo; while:
    >
    > int *(*(*bar)[20])(char c);
    >
    > Creates a variable bar with that same type?


    Yes.

    > (Not sure what the 'c' is doing in there).


    Documentation.

    >
    > How would I specify that same complex type without either the foo or bar
    > tag, as in a cast for example? Is it just:
    >
    > (int *(*(*)[20])(char c))
    >


    I *think* that's right, but I won't swear to it. Hopefully you should
    never need such a beast; if you do, that's where the typedef would
    save your bacon. Bear in mind, that's a deliberately pathological,
    made-up example to show where using a #define instead of a typedef
    would fail.

    > ?
    >
    > --
    > Bart
     
    John Bode, Mar 4, 2008
    #9
  10. vivek

    Morris Dovey Guest

    Micah Cowan wrote:
    >
    > Morris Dovey <> writes:


    > > Heh - might as well invoke all the demons...

    >
    > Ah, so _those_ are what flew out my nose when I forgot to ensure
    > isalpha()'s argument was representable as an unsigned char.


    Been wondering what you've been doing <g>

    I thought it might've been all my fault - I've been developing a
    DS.009 whose stdin, stdout, stderr, root partition, <everything>
    is hot-swappable (see web page at sig) so that they need to be
    continually "rediscovered" - and I've been suspecting that our
    favorite redhead might have expanded red/black to
    red/orange/yellow/green/blue/black and promulgated severe
    dimensional instabilities leading to the strange conjunction of
    all moons in the solar system waxing to fullness at the same
    instant. :)

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto/Projects/Monitor/
     
    Morris Dovey, Mar 4, 2008
    #10
  11. vivek

    Micah Cowan Guest

    Morris Dovey <> writes:

    > John Bode wrote:
    >>
    >> On Mar 4, 11:44 am, Kaz Kylheku <> wrote:
    >> > On Mar 4, 2:56 am, vivek <> wrote:
    >> >
    >> > > What will happen if i replace a typedef with a #define?
    >> >
    >> > Nothing of consequence.
    >> >
    >> > Eminent computer scientist S**** N**** demonstrated, years ago, that
    >> > they are the same thing.
    >> >
    >> > :)

    >>
    >> DO NOT SPEAK THAT NAME, lest he notice and come back to pester us
    >> again, with the Three Stooges (Twink, McCormack, and Just Richard)
    >> egging him on.

    >
    > Heh - might as well invoke all the demons...


    Ah, so _those_ are what flew out my nose when I forgot to ensure
    isalpha()'s argument was representable as an unsigned char.

    --
    Micah J. Cowan
    Programmer, musician, typesetting enthusiast, gamer...
    http://micah.cowan.name/
     
    Micah Cowan, Mar 4, 2008
    #11
  12. vivek

    John Bode Guest

    On Mar 4, 2:20 pm, Micah Cowan <> wrote:
    > Morris Dovey <> writes:
    > > John Bode wrote:

    >
    > >> On Mar 4, 11:44 am, Kaz Kylheku <> wrote:
    > >> > On Mar 4, 2:56 am, vivek <> wrote:

    >
    > >> > > What will happen if i replace a typedef with a #define?

    >
    > >> > Nothing of consequence.

    >
    > >> > Eminent computer scientist S**** N**** demonstrated, years ago, that
    > >> > they are the same thing.

    >
    > >> > :)

    >
    > >> DO NOT SPEAK THAT NAME, lest he notice and come back to pester us
    > >> again, with the Three Stooges (Twink, McCormack, and Just Richard)
    > >> egging him on.

    >
    > > Heh - might as well invoke all the demons...

    >
    > Ah, so _those_ are what flew out my nose when I forgot to ensure
    > isalpha()'s argument was representable as an unsigned char.
    >


    Gesundheit.
     
    John Bode, Mar 4, 2008
    #12
  13. vivek wrote:
    >
    > What will happen if i replace a typedef with a #define?


    Your program will fail to compile, your girlfriend will leave you,
    your hair will fall out, and your father will smell of elderberry
    wine.

    Or simply consider what happens if you replace:

    typedef char *CHAR_PTR;
    with
    #define CHAR_PTR char *

    and your program includes:

    CHAR_PTR pt1, pt2;

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Mar 4, 2008
    #13
  14. vivek <> writes:
    > What will happen if i replace a typedef with a #define?


    Regret.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 5, 2008
    #14
  15. vivek

    Richard Bos Guest

    Morris Dovey <> wrote:

    > John Bode wrote:
    > >
    > > On Mar 4, 11:44 am, Kaz Kylheku <> wrote:
    > > > On Mar 4, 2:56 am, vivek <> wrote:
    > > >
    > > > > What will happen if i replace a typedef with a #define?
    > > >
    > > > Nothing of consequence.
    > > >
    > > > Eminent computer scientist S**** N**** demonstrated, years ago, that
    > > > they are the same thing.
    > > >
    > > > :)

    > >
    > > DO NOT SPEAK THAT NAME, lest he notice and come back to pester us
    > > again, with the Three Stooges (Twink, McCormack, and Just Richard)
    > > egging him on.

    >
    > Heh - might as well invoke all the demons...
    >
    > What ever happened to that lad who preached that hexadecimal was
    > the one true number system?


    I don't know, but have you SEEN the CHAIR recently?

    Richard
     
    Richard Bos, Mar 5, 2008
    #15
  16. vivek

    ais523 Guest

    Bartc wrote:
    >
    > Are you saying that:
    >
    > typedef int *(*(*foo)[20])(char c);
    >
    > Creates a complex type with an alias of foo; while:
    >
    > int *(*(*bar)[20])(char c);
    >
    > Creates a variable bar with that same type? (Not sure what the 'c' is doing
    > in there).
    >
    > How would I specify that same complex type without either the foo or bar
    > tag, as in a cast for example? Is it just:
    >
    > (int *(*(*)[20])(char c))
    >
    > ?

    It's (int *(*(*)[20])(char)) because you can create a cast from a
    declaration by removing the identifiers in it that are referring to
    variables and parameters (unless someone has decided to do #define c
    volatile, or something like that, in which case the c would be
    required because it's qualifying the char that each of the functions
    takes as an argument, but generally speaking it can be assumed that
    the preprocessor isn't involved in this sort of question). (I'm not
    sure if (without the #define) it works as char c as well as with char,
    though.) If I ever had to write that, I'd explain what it did in a
    comment, though, I suspect (unless it was for the IOCCC). The only
    reason I can think of for using that cast right now would be
    dereferencing a pointer that was actually pointing to that bizarre
    collection of function pointers but was stored as a void* for some
    reason, but in such a case there's something to be said for assigning
    it to a variable of the right type and dereferencing that so as to
    make use of automatic void* conversion, and to not get confused. Oh,
    also casting the return value of malloc, but that's a bad idea anyway
    unless for some reason you want your C code to also be valid C++ (and
    normally you don't).
    --
    ais523
     
    ais523, Mar 5, 2008
    #16
  17. vivek

    Morris Dovey Guest

    Richard Bos wrote:

    > I don't know, but have you SEEN the CHAIR recently?


    I'm obviously missing something (either I've been away too long
    or didn't get enough sleep last night) - what have I missed?
    (e-mail is ok if you prefer).

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto/
     
    Morris Dovey, Mar 5, 2008
    #17
  18. vivek

    Morris Dovey Guest

    Richard Bos wrote:

    > I don't know, but have you SEEN the CHAIR recently?


    I'm obviously missing something (either I've been away too long
    or didn't get enough sleep last night) - what have I missed?
    (e-mail is ok if you prefer).

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto/
     
    Morris Dovey, Mar 5, 2008
    #18
  19. vivek

    Richard Bos Guest

    Morris Dovey <> wrote:

    > Richard Bos wrote:
    >
    > > I don't know, but have you SEEN the CHAIR recently?

    >
    > I'm obviously missing something (either I've been away too long
    > or didn't get enough sleep last night) - what have I missed?
    > (e-mail is ok if you prefer).


    Xney Z., our erstwhile local KOOK-IN-ITSELF.

    Richard
     
    Richard Bos, Mar 5, 2008
    #19
  20. Morris Dovey said:

    > Richard Bos wrote:
    >
    >> I don't know, but have you SEEN the CHAIR recently?

    >
    > I'm obviously missing something (either I've been away too long
    > or didn't get enough sleep last night) - what have I missed?


    Don't you remember Karl Malbrain? You replied to him at least once...

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Mar 5, 2008
    #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. O Plameras
    Replies:
    10
    Views:
    550
    Keith Thompson
    Dec 19, 2005
  2. ramu

    #define and typedef

    ramu, Jan 18, 2006, in forum: C Programming
    Replies:
    2
    Views:
    324
    Jaspreet
    Jan 18, 2006
  3. robin liu
    Replies:
    3
    Views:
    827
    Robin Liu
    Apr 21, 2006
  4. Replies:
    13
    Views:
    595
    Dietmar Schindler
    Feb 12, 2007
  5. oor
    Replies:
    0
    Views:
    1,364
Loading...

Share This Page