typedef question

Discussion in 'C Programming' started by James Brown, Nov 16, 2005.

  1. James Brown

    James Brown Guest

    I am defining the following typedefs:

    typedef int* pint;
    typedef pint* ppint;
    typedef ppint* pppint;

    taking the last typedef - "pppint" - would this be referred to as:

    an alias to type "ppint *" or
    an alias to type "int ***"

    Or rather, how is it treated by the compiler and the language?

    I think it's the latter, could anyone just clarify this please? The former
    would produce a kind of "type hierarchy" which chained typedef's together
    in a "parent/child" relationship, whereas the latter is just a simple
    type-renaming
    which always refers to the base type.

    (I am writing a _very_ simple compiler which just parses basic
    types+typedefs
    and want to represent types in the same way a C compiler would).

    Thanks,
    James
     
    James Brown, Nov 16, 2005
    #1
    1. Advertising

  2. James Brown

    Michael Mair Guest

    James Brown wrote:
    > I am defining the following typedefs:
    >
    > typedef int* pint;
    > typedef pint* ppint;
    > typedef ppint* pppint;
    >
    > taking the last typedef - "pppint" - would this be referred to as:
    >
    > an alias to type "ppint *" or
    > an alias to type "int ***"
    >
    > Or rather, how is it treated by the compiler and the language?
    >
    > I think it's the latter, could anyone just clarify this please? The former
    > would produce a kind of "type hierarchy" which chained typedef's together
    > in a "parent/child" relationship, whereas the latter is just a simple
    > type-renaming
    > which always refers to the base type.
    >
    > (I am writing a _very_ simple compiler which just parses basic
    > types+typedefs
    > and want to represent types in the same way a C compiler would).


    It is not important how you represent types at an intermediate
    stage; in the end, however, you have to arrive at "int ***".
    What you do in between is entirely yours to say, as there is no
    special translation phase dedicated to resolving typedefs
    specified in the standard.

    Some remarks:
    - typedef has the same syntax as a storage class specifier, i.e.
    you could replace it by "extern" or "register".
    - typedef int *pint;
    const pint p;
    gives you the same effect as
    int * const p;
    - if you want to exactly know how a compiler does it, then get
    the source of one and have a look at it
    - if you want to know what demands are there around typedef, have
    a look at the C99 last public draft (google for N869) or the C05
    last public draft (N1124) or the C89 one
    (http://danpop.home.cern.ch/danpop/ansi.c)

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Nov 16, 2005
    #2
    1. Advertising

  3. James Brown

    Skarmander Guest

    James Brown wrote:
    > I am defining the following typedefs:
    >
    > typedef int* pint;
    > typedef pint* ppint;
    > typedef ppint* pppint;
    >
    > taking the last typedef - "pppint" - would this be referred to as:
    >
    > an alias to type "ppint *" or
    > an alias to type "int ***"
    >
    > Or rather, how is it treated by the compiler and the language?
    >

    It would be *referred* to as an alias for "ppint*", but it would not be
    *wrong* to call it an alias for "int***".

    > I think it's the latter, could anyone just clarify this please? The former
    > would produce a kind of "type hierarchy" which chained typedef's together
    > in a "parent/child" relationship, whereas the latter is just a simple
    > type-renaming
    > which always refers to the base type.
    >

    Typedef just creates a synonym. There is no relationship, and strictly
    speaking there is no base type either (though treating it like this is
    not wrong, since it has the same effect). That is, after
    typedef a b;
    There is still just one type: the type represented by 'a'. It now just
    has an alternate name: 'b'. And after
    typedef a b;
    typedef b c;
    'a', 'b' and 'c' all refer to exactly the same type. You can call 'a'
    the "base type" because it came first, but it has no special status.
    Likewise 'b' is not more special than 'c', since we could have just as
    well written
    typedef a c;

    When declaring objects of these types:
    a x;
    b y;
    We would still say that x has type 'a' and y has type 'b', even if they
    designate the same type, and we would not typically say that y has type
    'a', although this is not wrong.

    > (I am writing a _very_ simple compiler which just parses basic
    > types+typedefs
    > and want to represent types in the same way a C compiler would).
    >

    Then separate types and names, but remember the name used for a type in
    a declaration (for diagnostics). Typedef declares a new name for an
    existing type, but does not otherwise have any influence on typing.

    Typedef is actually slightly trickier than I describe (for example,
    typedefs have scope, and you can declare multiple type names with one
    typedef), and for the full story you should read the standard, but
    that's the gist of it.

    S.
     
    Skarmander, Nov 16, 2005
    #3
  4. James Brown

    James Brown Guest

    "Skarmander" <> wrote in message
    news:437b8695$0$11064$4all.nl...
    > James Brown wrote:
    >> I am defining the following typedefs:
    >>
    >> typedef int* pint;
    >> typedef pint* ppint;
    >> typedef ppint* pppint;
    >>
    >> taking the last typedef - "pppint" - would this be referred to as:
    >>
    >> an alias to type "ppint *" or
    >> an alias to type "int ***"
    >>
    >> Or rather, how is it treated by the compiler and the language?
    >>

    > It would be *referred* to as an alias for "ppint*", but it would not be
    > *wrong* to call it an alias for "int***".
    >
    >> I think it's the latter, could anyone just clarify this please? The
    >> former
    >> would produce a kind of "type hierarchy" which chained typedef's together
    >> in a "parent/child" relationship, whereas the latter is just a simple
    >> type-renaming
    >> which always refers to the base type.
    >>

    > Typedef just creates a synonym. There is no relationship, and strictly
    > speaking there is no base type either (though treating it like this is not
    > wrong, since it has the same effect). That is, after
    > typedef a b;
    > There is still just one type: the type represented by 'a'. It now just has
    > an alternate name: 'b'. And after
    > typedef a b;
    > typedef b c;
    > 'a', 'b' and 'c' all refer to exactly the same type. You can call 'a' the
    > "base type" because it came first, but it has no special status. Likewise
    > 'b' is not more special than 'c', since we could have just as well written
    > typedef a c;
    >
    > When declaring objects of these types:
    > a x;
    > b y;
    > We would still say that x has type 'a' and y has type 'b', even if they
    > designate the same type, and we would not typically say that y has type
    > 'a', although this is not wrong.
    >
    >> (I am writing a _very_ simple compiler which just parses basic
    >> types+typedefs
    >> and want to represent types in the same way a C compiler would).
    >>

    > Then separate types and names, but remember the name used for a type in a
    > declaration (for diagnostics). Typedef declares a new name for an existing
    > type, but does not otherwise have any influence on typing.
    >
    > Typedef is actually slightly trickier than I describe (for example,
    > typedefs have scope, and you can declare multiple type names with one
    > typedef), and for the full story you should read the standard, but that's
    > the gist of it.
    >
    > S.


    thanks (and also to Michael), I think I've got it now!

    James
     
    James Brown, Nov 16, 2005
    #4
    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. JustSomeGuy
    Replies:
    1
    Views:
    337
    Mike Wahler
    Sep 20, 2003
  2. Tony Johansson
    Replies:
    7
    Views:
    349
    Victor Bazarov
    Aug 16, 2005
  3. Alex

    typedef question

    Alex, Nov 28, 2005, in forum: C++
    Replies:
    6
    Views:
    465
    Alex O.
    Dec 2, 2005
  4. John

    typedef question

    John, Aug 18, 2006, in forum: C++
    Replies:
    5
    Views:
    492
    Frederick Gotham
    Aug 18, 2006
  5. oor
    Replies:
    0
    Views:
    1,357
Loading...

Share This Page