Connecting Tech Pros Worldwide Help | Site Map

typedef question

James Brown
Guest
 
Posts: n/a
#1: Nov 16 '05
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


Michael Mair
Guest
 
Posts: n/a
#2: Nov 16 '05

re: typedef question


James Brown wrote:[color=blue]
> 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).[/color]

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.
Skarmander
Guest
 
Posts: n/a
#3: Nov 16 '05

re: typedef question


James Brown wrote:[color=blue]
> 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?
>[/color]
It would be *referred* to as an alias for "ppint*", but it would not be
*wrong* to call it an alias for "int***".
[color=blue]
> 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.
>[/color]
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.
[color=blue]
> (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).
>[/color]
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.
James Brown
Guest
 
Posts: n/a
#4: Nov 16 '05

re: typedef question



"Skarmander" <invalid@dontmailme.com> wrote in message
news:437b8695$0$11064$e4fe514c@news.xs4all.nl...[color=blue]
> James Brown wrote:[color=green]
>> 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?
>>[/color]
> It would be *referred* to as an alias for "ppint*", but it would not be
> *wrong* to call it an alias for "int***".
>[color=green]
>> 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.
>>[/color]
> 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.
>[color=green]
>> (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).
>>[/color]
> 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.[/color]

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

James


Closed Thread


Similar C / C++ bytes