I'm trying to understand why people usually cast the address of a C pointer when associating it to a variable. For example:
int *x
x = (int*) malloc (sizeof(int));
What's the point of the (int*) casting there?
Another example:
int *x;
char *c;
x = (int*) malloc (sizeof(int));
*x = 100;
c = (char*) x;
Again, what's the point of the (int*) cast in the malloc and, now, the cast (char*) ?
The size of the adresses should not change depending on the type of the pointer. In my understanding the int* and char* (pointers) should take the same amount of memory, because the addresses of both should have the same size, regardless if the pointer is pointing to an integer or a character in the memory. That said, I don't see why this cast is necessary, since I don't see how this could be useful to the compiler.
Also, let me add a similar question that might help me to understand my first question:
For example, What's the difference between:
x = (int *) malloc(sizeof(int));
x = (char *) malloc(sizeof(int));
If someone could explain me what's the point of these casts, I would be grateful.