#define vs typedef

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sophia

    #define vs typedef

    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_r oyce;

    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.

  • Jack Klein

    #2
    Re: #define vs typedef

    On Sun, 27 Apr 2008 00:44:41 -0700 (PDT), sophia
    <sophia.agnes@g mail.comwrote in comp.lang.c:
    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_r oyce;
    >
    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.
    cv qualifiers (and possibly the restrict qualifier, haven't thought
    about it) can't "penetrate" a typedef, they can only apply to the
    object defined.

    Example:

    typedef char *cptr;
    #define CPTR char *

    int main(void)
    {
    char junk [] = "abcdefghij ";
    const cptr cp1 = junk;
    cptr const cp2 = junk;
    const CPTR cp3 = junk;
    CPTR const cp4 = junk;
    *cp1 = '1'; /* line 11, cp1 points to non-const char */
    *cp2 = '2'; /* line 12, cp2 points to non-const char */
    *cp3 = '3'; /* line 13, cp3 points to const char */
    *cp4 = '4'; /* line 14, cp4 points to non-const char */
    ++cp1; /* line 15, cp1 pointer is const */
    ++cp2; /* line 16, cp2 pointer is const */
    ++cp3; /* line 17, cp3 pointer is non-const */
    ++cp4; /* line 18, cp4 pointer is const */
    }

    Here's the output from Visual C++ 6:

    junk.c(13) : error C2166: l-value specifies const object
    junk.c(15) : error C2166: l-value specifies const object
    junk.c(16) : error C2166: l-value specifies const object
    junk.c(18) : error C2166: l-value specifies const object

    So putting the const keyword either before or after a typedef'ed
    pointer type does not make it a pointer to const" as one might think.
    Instead the pointer being defined, is a constant pointer to
    non-constant.

    On the other hand, adding the const keyword to the CPTR macro makes is
    a non-const pointer to const car, if used before the macro, or a const
    pointer to non-const char, if used after.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.l earn.c-c++

    Comment

    Working...