Yesterday I was in class, and at some point the instructor was talking about C code. He said:
What is the purpose of making a pointer cast in C? The only purpose is to make the compiler interpret correctly the pointer operations (for example, adding an int pointer will result in a different offset than adding a char pointer). Apart from that, there is no difference: all pointers are represented the same way in memory, regardless if the pointer is pointing to an int value, a char value, a short value, or whatever. So, casting a pointer will not modify anything in the memory, it will just help the programmer with operations more related with the pointer-type he is dealing with.
However, I have read, specially here in Stack Overflow, that this is not 100% true. I have read that in some weird machines, pointers for different types can be stored in different ways in memory. In this case, not changing the pointer to the correct type could cause problems if the code is compiled to this kind of machine.
Basically, this is the kind of code I'm talking about. Consider the code below:
int* int_pointer;
char* char_pointer;
int_pointer = malloc(sizeof(int));
*int_pointer = 4;
And now two options:
1.
char_pointer = (char *)int_pointer;
2.
char_pointer = int_pointer;
The code on case 2 could became a problem? Making the cast (case 1) would eventually change the pointer format in memory (if yes, could you give an example of machine?)?
Thanks