#define vs typedef

Discussion in 'C Programming' started by sophia, Apr 27, 2008.

  1. sophia

    sophia Guest

    Dear all,

    the following are the differences b/w #define and typedef ,which i
    have seen in Peter van der lindens book. is there any other difference
    between thes two ?



    The right way to think about typedef as being a complete encapsulated
    type - you can't add to it after you have declared it.

    ex:-

    #define peach int
    unsigned peach i; /*works fine*/

    typedef int banana ;
    unsigned banana i; /*illegal*/

    a typedef'd name provides the type for every declarator in a
    declaration

    Ex:-

    #define int_ptr int*
    int_ptr chalk, cheese;

    after macro expansion, the second line effectively becomes
    int* chalk,cheese;

    In contrast a typedef like this:

    typedef char* char_ptr;
    char_ptr bentley,rolls_royce;

    declares both bentley and rolls_royce to be the same . the name on the
    front is different, but they are both a pointer to a char.
     
    sophia, Apr 27, 2008
    #1
    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:
    560
    Keith Thompson
    Dec 19, 2005
  2. ramu

    #define and typedef

    ramu, Jan 18, 2006, in forum: C Programming
    Replies:
    2
    Views:
    327
    Jaspreet
    Jan 18, 2006
  3. robin liu
    Replies:
    3
    Views:
    863
    Robin Liu
    Apr 21, 2006
  4. Vladimir Oka

    define versus typedef

    Vladimir Oka, Jun 8, 2006, in forum: C Programming
    Replies:
    10
    Views:
    536
    Christian Christmann
    Jun 8, 2006
  5. oor
    Replies:
    0
    Views:
    1,411
Loading...

Share This Page