J
jacob navia
Hi
I am still writing my tutorial book about C. Here is the section about
casts. I would be interested in your opinions about this. Some people
have definite views about this subject ("never use casts") some others
(like me) are more "liberal" in this subject. I would be interested
in any feedback.
jacob
-------------------------------------------------------------------
Casts
A cast expression is the transformation of an object from one type to
another. For instance, a common need is to transform double precision
numbers into integers. This is specified like this:
double d;
....
(int)d
In this case, the cast needs to invoke run time code to make the actual
transformation. In other cases there is no code emitted at all. For
instance in:
void *p;
....
(char *)p;
Transforming one type of pointer into another needs no code at all at
run-time in most implementations.
When to use casts
A cast can be useful to avoid unnecessary operations. For instance,
given the declarations:
double double_value;
int integer_value;
If you write
integer_value /= double_value;
The integer is promoted to double to do the division, then the result is
converted to integer again. With a cast however, the unnecessary
promotions can be avoided:
integer_value /= (int)double_value;
You can use a cast expression in another context, to indicate the type
of a composite constant literal. For instance:
typedef struct tagPerson {
char Name[75];
int age;
} Person;
void process(Person *);
....
process(&(Person){“Mary Smith” , 38});
This is one of the new features of C99. The literal should be enclosed
in braces, and it should match the expected structure. This is just
“syntactic sugar” for the following:
Person __998815544ss = { “Mary Smith”, 38};
process(&__998815544ss);
The advantage is that now you are spared that task of figuring out a
name for the structure since the compiler does that for you. Internally
however, that code represents exactly what happens inside lcc-win32.
When not to use casts
Casts, as any other of the constructs above, can be misused. In general,
they make almost impossible to follow the type hierarchy automatically.
C is weakly typed, and most of the “weakness” comes from casts expressions.
----------------------------------------------------------------------
I am still writing my tutorial book about C. Here is the section about
casts. I would be interested in your opinions about this. Some people
have definite views about this subject ("never use casts") some others
(like me) are more "liberal" in this subject. I would be interested
in any feedback.
jacob
-------------------------------------------------------------------
Casts
A cast expression is the transformation of an object from one type to
another. For instance, a common need is to transform double precision
numbers into integers. This is specified like this:
double d;
....
(int)d
In this case, the cast needs to invoke run time code to make the actual
transformation. In other cases there is no code emitted at all. For
instance in:
void *p;
....
(char *)p;
Transforming one type of pointer into another needs no code at all at
run-time in most implementations.
When to use casts
A cast can be useful to avoid unnecessary operations. For instance,
given the declarations:
double double_value;
int integer_value;
If you write
integer_value /= double_value;
The integer is promoted to double to do the division, then the result is
converted to integer again. With a cast however, the unnecessary
promotions can be avoided:
integer_value /= (int)double_value;
You can use a cast expression in another context, to indicate the type
of a composite constant literal. For instance:
typedef struct tagPerson {
char Name[75];
int age;
} Person;
void process(Person *);
....
process(&(Person){“Mary Smith” , 38});
This is one of the new features of C99. The literal should be enclosed
in braces, and it should match the expected structure. This is just
“syntactic sugar” for the following:
Person __998815544ss = { “Mary Smith”, 38};
process(&__998815544ss);
The advantage is that now you are spared that task of figuring out a
name for the structure since the compiler does that for you. Internally
however, that code represents exactly what happens inside lcc-win32.
When not to use casts
Casts, as any other of the constructs above, can be misused. In general,
they make almost impossible to follow the type hierarchy automatically.
C is weakly typed, and most of the “weakness” comes from casts expressions.
----------------------------------------------------------------------