A cast does not make any changes to the data. It can be thought of as a signal to the compiler that "yes I know that x and y are different types, but do the assignment anyway!". The compiler then arranges for a bit-wise copy from x to y. If you do get a change in value, it will be because of the difference in size of the source and target eg
int i = 256;
char c = i;
You do need to be careful when casting between pointers of different types. In general, a char pointer (a pointer to a single byte), has no alignment requirements, but other types, say an int, may require that it is aligned on a 4 byte boundary (or 2, 8 or 16, etc depending on the type). In that case, you might get a segfault when trying to dereference a pointer. e.g.
char str[5] = { 0x00, 0x00, 0x00, 0x00, 0x0 };
int *i = (int *)&(str[0]);
printf("*i = %d\n", *i);
i = (int*)(&str[1]);
printf("*i = %d\n", *i);
Given the constraints above, one of the two printf statements will produce a seg fault, as one or the other of the int pointers are mis-aligned as per the CPU requirements. So you need to be careful what you do when casting pointers from one type to another. Note that
malloc()
and related calls are guaranteed to return a pointer that is correctly aligned for any type, so you have no problems doing
type *ptr = (type*)malloc(sizeof(*ptr) * 42)