Вы находитесь на странице: 1из 2

When should a type cast be used?

There are two situations in which to use a type cast.


The first use is to change the type of an operand to an arithmetic operation so
that the operation will be performed properly.
The second case is to cast pointer types to and from void * in order to interfac
e with functions that expect or return void pointers. For example, the following
line type casts the return value of the call to malloc() to be a pointer to a f
oo structure.
struct foo *p = (struct foo *) malloc(sizeof(struct foo));
A type cast should not be used to override a const or volatile declaration. Over
riding these type modifiers can cause the program to fail to run correctly. A ty
pe cast should not be used to turn a pointer to one type of structure or data ty
pe into another. In the
rare events in which this action is beneficial, using a union to hold the values
makes the programmer?s intentions clearer.
Whats short-circuiting in C expressions?
What this means is that the right hand side of the expression is not evaluated i
f the left hand side determines the outcome. That is if the left hand side is tr
ue for || or false for &&, the right hand side is not evaluated.
Whats wrong with the expression a[i]=i++; ? Whats a sequence point?
Although its surprising that an expression like i=i+1; iscompletely valid, somethi
ng like a[i]=i++; is not. This is because all accesses to an element must be to
change the value of that variable. In the statement a[i]=i++; , the access to i
is not for itself, but for a[i] and so its invalid. On similar lines, i=i++; or
i=++i; are invalid. If you want to increment the value of i, use i=i+1; or i+=1;
or i++; or ++i; and not some combination.
A sequence point is a state in time (just after the evaluation of a full express
ion, or at the ||, &&, ?:, or comma operators, or just before a call to a functi
on) at which there are no side effects.
The ANSI/ISO C Standard states that
Between the previous and next sequence point an object shall have its stored
value modified at most once by the evaluation of an expression. Furthermore,
the prior value shall be accessed only to determine the value to be stored.
At each sequence point, the side effects of all previous expressions will be com
pleted. This is why you cannot rely on expressions such as a[i] = i++;, because
there is no sequence point specified for the assignment, increment or index oper
ators, you don't know when the effect of the increment on i occurs.
The sequence points laid down in the Standard are the following:
*
*
*
*
*
*

The point of calling a function, after evaluating its arguments.


The end of the first operand of the && operator.
The end of the first operand of the || operator.
The end of the first operand of the ?: conditional operator.
The end of the each operand of the comma operator.
Completing the evaluation of a full expression. They are the following:

o
o
o
o
o

Evaluating the initializer of an auto object.


The expression in an ?ordinary? statement?an expression followed by semicolon.
The controlling expressions in do, while, if, switch or for statements.
The other two expressions in a for statement.
The expression in a return statement.

Вам также может понравиться