126
6.6. Referring to a Type with
Another way to refer to the type of an expression is with
looks like
, but the construct acts semantically like a type name defined with
sizeof
There are two ways of writing the argument to
example with an expression:
typeof (x[0](1))
This assumes that
is an array of pointers to functions; the type described is that of the values of the
x
functions.
Here is an example with a typename as the argument:
typeof (int *)
Here the type described is that of pointers to
If you are writing a header file that must work when included in ISO C programs, write
instead of
. Section 6.41 Alternate Keywords.
typeof
A
-construct can be used anywhere a typedef name could be used. For example, you can use
typeof
it in a declaration, in a cast, or inside of
is often useful in conjunction with the statements-within-expressions feature. Here is how the
typeof
two together can be used to define a safe "maximum" macro that operates on any arithmetic type and
evaluates each of its arguments exactly once:
#define max(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a
_b ? _a : _b; })
The reason for using names that start with underscores for the local variables is to avoid conflicts with
variable names that occur within the expressions that are substituted for
to design a new form of declaration syntax that allows you to declare variables whose scopes start
only after their initializers; this will be a more reliable way to prevent such conflicts.
Some more examples of the use of
This declares
with the type of what
•
y
typeof (*x) y;
This declares
as an array of such values.
•
y
typeof (*x) y[4];
This declares
as an array of pointers to characters:
•
y
typeof (typeof (char *)[4]) y;
It is equivalent to the following traditional C declaration:
char *y[4];
To see the meaning of the declaration using
let's rewrite it with these macros:
#define pointer(T)
#define array(T, N) typeof(T [N])
Now the declaration can be rewritten this way:
array (pointer (char), 4) y;
Thus,
array (pointer (char), 4)
Chapter 6. Extensions to the C Language Family
typeof
typeof
.
int
or
sizeof
:
typeof
points to.
x
typeof
typeof(T *)
is the type of arrays of 4 pointers to
. The syntax of using of this keyword
typeof
: with an expression or with a type. Here is an
.
typeof
, and why it might be a useful way to write,
typedef
__typeof__
and
. Eventually we hope
a
b
.
char
.
Need help?
Do you have a question about the ENTERPRISE LINUX 3 - USING GCC and is the answer not in the manual?
Questions and answers