New names or aliases for existing types may be created
using typedef
. For example:
typedef int int32_t;
This allows int32_t
to be used
anywhere in place of int
.
Such aliases are often used to hide implementation- or
platform-specific details, or to allow the choice of a
widely-used type to be changed easily.
typedef
s are also useful
for expressing complex compound types. For example, a
prototype for the standard-library function signal
has the following,
rather cryptic form (in ISO C):
void (*signal(int signum, void (*handler)(int)))(int);
Erm, what? It becomes a little clearer when POSIX (an Operating System standard which incorporates the C standard) declares it:
typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler);
Now we can see that the function's second parameter has the same type as its return value, and that that type is, in fact, a pointer-to-function type.
Note that a typedef
is
syntactically similar to a variable declaration,
with the new type name appearing in the place of the
variable name.
There is no equivalent of type aliasing in Java.