In C, theresult of the cast operation is not an lvalue.
#include <stdlib.h>
int main(void) {
int* myArray = (int*) malloc(10 * sizeof(int));
free(myArray);
return 0;
}
The malloc library function returns
a void pointer that points to memory that holds an
object of the size of its argument. The statement int* myArray
= (int*) malloc(10 * sizeof(int)) has the following steps: Casting to a union type is the ability to cast a union member to the same type as the union to which it belongs. Such a cast does not produce an lvalue. The feature is supported as an extension to C99, implemented to facilitate porting programs developed with GNU C.
#include <stdio.h>
union f {
char t;
short u;
int v;
long w;
long long x;
float y;
double z;
};
int main() {
union f u;
char a = 1;
u = (union f)a;
printf("u = %i\n", u.t);
}
The output of this example is: u = 1
int main() {
union u_t {
char a;
short b;
int c;
union u2_t {
double d;
}u2;
};
union u_t U;
double dd = 1.234;
U.u2 = (union u2_t) dd; // Valid.
printf("U.u2 is %f\n", U.u2);
}
The output of this example is: U.u2 is 1.234
struct S{
int a;
}s;
union U{
struct S *s;
};
struct T{
union U u;
};
static struct T t[] = { {(union U)&s} };