The common coding practice of casting a pointer to an incompatible type and then dereferencing it violates this rule. (Note that char pointers are an exception to this rule) Refer to the description of the ANSIALIAS option in the z/OS® XL C/C++ User's Guide for additional information.
int *p;
double d = 0.0;
int *faa(double *g); /* cast operator inside the function */
void foo(double f) {
p = faa(&f); /* turning &f into an int ptr */
f += 1.0; /* The optimizer might move the */
/* assignment after the printf statement. */
printf("f=%x\n", *p);
}
int *faa(double *g) { return (int*) g; } /* questionable cast; */
/* the function can be in */
/* another translation unit */
int main() {
foo(d);
}
In the above printf statement, *p cannot
be dereferenced to a double under the ANSI aliasing
rule. The compiler determines that the result of f
+= 1.0 does not affect the value of *p.
Thus, the optimizer might move the assignment after the printf statement. If
you compile the above example with optimization enabled, the printf statement
might output 0 (zero).