B
BartC
Again, typedef creates a new name for an existing type; it does not
create a new type (unless you mangle the meaning of "create", "new",
and/or "type").
If the existing type is specified with a simple name, then typedef creates
an alias, or synonym for that type.
If the 'existing' type is more elaborate than that, then you are
constructing a type spec, built using pointers, arrays, structs,
size/const/width/storage attributes, functions (with argument types and
return values), built-in types, and previous typedef names.
This type-spec may or may not have previously been constructed elsewhere in
the source (using another typedef, specified in full, or some combination);
but it doesn't matter, because semantically the underlying typespec (what
you get when flatten out any typedef names) is treated the same way by the
compiler.
The typedef is useful in source code, as a convenient way of re-using a
complex typespec in many places (where #defines won't work because of the
convoluted nature of C typespecs), for assembling complex typespecs in
stages, and for abstracting a particular typespec.
(I've some experience of compiling typedef-like statements in my own
language:
* My 'typedef' statement (which I just call 'type'), *does* create a
distinct type, which creates all sorts of problems, with type-matching,
overloading, inheritance, and all that sort of thing (most of which I just
ignore, and which C has done well to steer clear of...)
* Having a 'typedef' that *doesn't* create a distinct type, as in C, is
actually more difficult. (Macros will sort of do it, as my typespecs are
linear, but have the same sort of scope problems as C. Besides I think
macros are a naff way of doing many things..
* The case where a new type name is defined simply in terms of another (eg.
typedef A B, which seems simple enough, I found created a few special
problems of its own. I've already said above I think this is a different
kind of typedef from the rest.)
My wife chose to change her last name when we got married. This did
not create (or even "define") a new person, merely a new name for
an existing person. (If she's a new person, it's only figuratively.)
That's not a good analogy for the C type system; there can only be one
physical instance of your wife, whether as Mrs Thompson, some either name,
or anonymous. But the same complex type spec can appear in many different
places in source code, either whole or in pieces, and each whole or part can
be typedefed or not.