Interesting features of #define and typedef, correct me if I am wrong

Discussion in 'C Programming' started by fdmfdmfdm@gmail.com, Feb 1, 2007.

  1. Guest

    Look at these two codes:

    ===================================
    #define int_ptr int*
    int_ptr a, b;
    ===================================

    and

    ===================================
    typedef int* int_ptr
    int_ptr a, b;
    ===================================

    In first example, only a is a pointer-to-an-integer but b is an
    integer.

    At second example, both of a, b are pointer-to-an-integer.

    Do you guys concur with me?
    , Feb 1, 2007
    #1
    1. Advertising

  2. <> wrote in message
    > Look at these two codes:
    >
    > ===================================
    > #define int_ptr int*
    > int_ptr a, b;
    > ===================================
    >
    > and
    >
    > ===================================
    > typedef int* int_ptr
    > int_ptr a, b;
    > ===================================
    >
    > In first example, only a is a pointer-to-an-integer but b is an
    > integer.
    >
    > At second example, both of a, b are pointer-to-an-integer.
    >
    > Do you guys concur with me?
    >

    That's one of the reasons you need typedef. The syntax for declaring
    pointers is a bit unusual, though it makes sense in a formal grammary sort
    of way.
    Malcolm McLean, Feb 1, 2007
    #2
    1. Advertising

  3. Ian Collins Guest

    Re: Interesting features of #define and typedef, correct me if Iam wrong

    Malcolm McLean wrote:
    > <> wrote in message
    >
    >>Look at these two codes:
    >>
    >>===================================
    >>#define int_ptr int*
    >>int_ptr a, b;
    >>===================================
    >>
    >>and
    >>
    >>===================================
    >>typedef int* int_ptr
    >>int_ptr a, b;
    >>===================================
    >>
    >>In first example, only a is a pointer-to-an-integer but b is an
    >>integer.
    >>
    >>At second example, both of a, b are pointer-to-an-integer.
    >>
    >>Do you guys concur with me?
    >>

    >
    > That's one of the reasons you need typedef. The syntax for declaring
    > pointers is a bit unusual, though it makes sense in a formal grammary sort
    > of way.
    >

    It's also a good reason for not declaring more than one variable per line.

    --
    Ian Collins.
    Ian Collins, Feb 1, 2007
    #3
  4. "Ian Collins" <> wrote in message
    news:...
    > Malcolm McLean wrote:
    >> <> wrote in message
    >>

    [OP]
    >>
    >> That's one of the reasons you need typedef. The syntax for declaring
    >> pointers is a bit unusual, though it makes sense in a formal grammary
    >> sort
    >> of way.
    >>

    > It's also a good reason for not declaring more than one variable per line.

    This post is typical of a variety that I don't really understand. It uses
    the idioms of C to do things that are ill-advised. Why? Because it's
    unclear and unnecessary. With preprocessor stuff and type definitions, the
    world is a better place when whitespace and carriage return delimit the
    statements liberally. What makes a #define statement interesting is the
    content of what is being defined, not the statement itself. LS
    Lane Straatman, Feb 1, 2007
    #4
  5. wrote:
    > ===================================
    > #define int_ptr int*
    > int_ptr a, b;
    > ===================================
    >
    > and
    >
    > ===================================
    > typedef int* int_ptr
    > int_ptr a, b;
    > ===================================
    >
    > In first example, only a is a pointer-to-an-integer but b is an
    > integer.
    >
    > At second example, both of a, b are pointer-to-an-integer.


    The base type is int. The '*' denotes the identifier as being a pointer to
    that base type.

    You're operating from the paradigm that the base type is pointer-to-int, which
    it isn't - it's int.
    Christopher Layne, Feb 2, 2007
    #5
  6. said:

    > Look at these two codes:
    >
    > ===================================
    > #define int_ptr int*
    > int_ptr a, b;
    > ===================================
    >
    > and
    >
    > ===================================
    > typedef int* int_ptr
    > int_ptr a, b;
    > ===================================
    >
    > In first example, only a is a pointer-to-an-integer but b is an
    > integer.
    >
    > At second example, both of a, b are pointer-to-an-integer.
    >
    > Do you guys concur with me?


    I don't understand why you think it's interesting.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Feb 2, 2007
    #6
  7. Nishu Guest

    On Feb 2, 11:36 am, Richard Heathfield <> wrote:

    <snip>
    > > ===================================
    > > #define int_ptr int*
    > > int_ptr a, b;
    > > ===================================

    >
    > > and

    >
    > > ===================================
    > > typedef int* int_ptr
    > > int_ptr a, b;
    > > ===================================

    >
    > > In first example, only a is a pointer-to-an-integer but b is an
    > > integer.

    >
    > > At second example, both of a, b are pointer-to-an-integer.

    >
    > > Do you guys concur with me?

    >
    > I don't understand why you think it's interesting.
    >


    Is it true that typedef increases code size but #define does_NOT_,
    since latter being the preprocessor.

    Thanks,
    Nishu
    Nishu, Feb 2, 2007
    #7
  8. Nishu said:

    <snip>

    > Is it true that typedef increases code size but #define does_NOT_,
    > since latter being the preprocessor.


    It depends on what you mean by code size. You might conceivably mean the
    source code, in which case of course they both increase it, just as any
    language construct does, including whitespace.

    But if you mean the object code, then no, it isn't true. For one thing,
    typedefs needn't increase code size. For another, #defines definitely can.
    Thirdly, #defines are not the preprocessor. They are merely expanded by the
    preprocessor. "Expanded" should give you an extra clue here.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Feb 2, 2007
    #8
  9. "Nishu" <> writes:
    [...]
    > Is it true that typedef increases code size but #define does_NOT_,
    > since latter being the preprocessor.


    No. A typedef merely creates an alias for an existing type. It's a
    notational convenience. I can't think of any reason why it would
    increase code size. If you can explain what you mean (increase code
    size compared to what?), perhaps we can explain further.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 2, 2007
    #9
  10. Nishu Guest

    On Feb 2, 12:58 pm, Keith Thompson <> wrote:
    > "Nishu" <> writes:
    >
    > [...]
    >
    > > Is it true that typedef increases code size but #define does_NOT_,
    > > since latter being the preprocessor.

    >
    > No. A typedef merely creates an alias for an existing type. It's a
    > notational convenience. I can't think of any reason why it would
    > increase code size. If you can explain what you mean (increase code
    > size compared to what?), perhaps we can explain further.


    By increase in code size, I meant increase in object code size (or the
    library size which includes least symbols(in release mode)) by using
    typedef instead of preprocessor.

    I've a notion that Preprocessors were simply replaced by Preprocessor
    before compiler/assembler generates the object code but for typedef
    there might be some implicit conversions since it is not a
    preprocessor.

    Thanks,
    Nishu
    Nishu, Feb 2, 2007
    #10
  11. jacob navia Guest

    Re: Interesting features of #define and typedef, correct me if Iam wrong

    Nishu a écrit :
    > On Feb 2, 12:58 pm, Keith Thompson <> wrote:
    >
    >>"Nishu" <> writes:
    >>
    >>[...]
    >>
    >>
    >>>Is it true that typedef increases code size but #define does_NOT_,
    >>>since latter being the preprocessor.

    >>
    >>No. A typedef merely creates an alias for an existing type. It's a
    >>notational convenience. I can't think of any reason why it would
    >>increase code size. If you can explain what you mean (increase code
    >>size compared to what?), perhaps we can explain further.

    >
    >
    > By increase in code size, I meant increase in object code size (or the
    > library size which includes least symbols(in release mode)) by using
    > typedef instead of preprocessor.
    >
    > I've a notion that Preprocessors were simply replaced by Preprocessor
    > before compiler/assembler generates the object code but for typedef
    > there might be some implicit conversions since it is not a
    > preprocessor.
    >
    > Thanks,
    > Nishu
    >


    You are wrong. The object code size will be the same, since the assembly
    instructions for using an int pointer or a typedefed int pointer will be
    exactly the same.

    Of course, if your compiler generates DEBUG INFORMATION then it will
    generate a record for the typedef. But many compilers (lcc-win32 for
    instance) generate ALSO records for the #defines, noting if it is a
    macro or just a replacement, etc. This will be highly specific to the
    format of the debug information but I would guess it will be almost the
    same.
    jacob navia, Feb 2, 2007
    #11
  12. "Nishu" <> wrote in message
    >
    >> No. A typedef merely creates an alias for an existing type. It's a
    >> notational convenience. I can't think of any reason why it would
    >> increase code size. If you can explain what you mean (increase code
    >> size compared to what?), perhaps we can explain further.

    >
    > By increase in code size, I meant increase in object code size (or the
    > library size which includes least symbols(in release mode)) by using
    > typedef instead of preprocessor.
    >
    > I've a notion that Preprocessors were simply replaced by Preprocessor
    > before compiler/assembler generates the object code but for typedef
    > there might be some implicit conversions since it is not a
    > preprocessor.
    >

    A compiler can do what it wants.
    So it is conceivable that it might add an ASCII string representing the
    typedef to some intermediate file, even the final executable. However
    normally typedef would be resolved to the basic type by the front end, and
    the object have the plain signature.
    Malcolm McLean, Feb 3, 2007
    #12
  13. "Nishu" <> wrote in message
    news:...
    > On Feb 2, 12:58 pm, Keith Thompson <> wrote:
    >> "Nishu" <> writes:
    >>
    >> [...]
    >>
    >> > Is it true that typedef increases code size but #define does_NOT_,
    >> > since latter being the preprocessor.

    >>
    >> No. A typedef merely creates an alias for an existing type. It's a
    >> notational convenience. I can't think of any reason why it would
    >> increase code size. If you can explain what you mean (increase code
    >> size compared to what?), perhaps we can explain further.

    >
    > By increase in code size, I meant increase in object code size (or the
    > library size which includes least symbols(in release mode)) by using
    > typedef instead of preprocessor.
    >
    > I've a notion that Preprocessors were simply replaced by Preprocessor
    > before compiler/assembler generates the object code but for typedef
    > there might be some implicit conversions since it is not a
    > preprocessor.

    #define verbose damnit
    I think it makes next to no sense to talk about code size with either a
    defines or a typedef. You take all the room you need. LS
    Lane Straatman, Feb 6, 2007
    #13
  14. Re: Interesting features of #define and typedef, correct me if I amwrong

    Lane Straatman wrote:
    > I think it makes next to no sense to talk about code size with either a
    > defines or a typedef. You take all the room you need. LS


    Yes, and there is no reason why increased room would be needed either
    way.

    --
    DPS
    Dietmar Schindler, Feb 12, 2007
    #14
    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:
    545
    Keith Thompson
    Dec 19, 2005
  2. robin liu
    Replies:
    3
    Views:
    821
    Robin Liu
    Apr 21, 2006
  3. Jonathan Mcdougall
    Replies:
    2
    Views:
    492
    Kaz Kylheku
    Nov 3, 2005
  4. BillJosephson
    Replies:
    148
    Views:
    2,634
    peter koch
    Jan 27, 2007
  5. oor
    Replies:
    0
    Views:
    1,341
Loading...

Share This Page