typedef question

J

James Brown

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
 
M

Michael Mair

James said:
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
 
S

Skarmander

James said:
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.
 
J

James Brown

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

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top