0

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.

felipeek
  • 1,193
  • 2
  • 10
  • 31
  • 4
    For part of the question, [you **don't** cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Yu Hao Jan 03 '15 at 16:04
  • 2
    A very duplicated, but very true question. Why the downvotes? – glglgl Jan 03 '15 at 16:21
  • @Yu Hao thanks, I've already read this question before I made mine. However, it still not clear to me the point of these castings, or, in other words, what they actually do. I made this question hoping someone could help me to understand it a little deeper – felipeek Jan 03 '15 at 16:23
  • 1. Pointers _can_ differ in representation (though `char *` and `void *` have the same representation), although such implementations are rare. 2. Pointers are strongly typed in C, only conversion from and to `void *` don't require a cast. 3. It doesn't make a difference if a conversion is triggered by a cast (or by an assignment, for example), some conversions require a cast, though. – mafso Jan 04 '15 at 14:36
  • @mafso Thanks for the answer. So, if there is no difference triggering a conversion by a cast, the only reason to do it is to avoid compiler warnings? I mean, the result of a pointer conversion using a cast or not will always be the same, then? Oh, and if pointers can differ in representation, like you said, then, in this case, wouldn't the cast be necessary to convert from types that have different representations? – felipeek Jan 05 '15 at 15:36
  • Some conversions can only be triggered by a cast, e.g. from `float *` to `int *` (all conversions involving a pointer type but conversion between `void *` and object pointer types require a cast). Some compilers may only give a warning (or nothing, though this isn't standard conforming) for a missing cast, in which case the conversion is still implicitly there in case of an assignment (or a function call, or an initialization, ...). A standard conforming implementation (like `gcc -std=c99 -pedantic-errors`) may also refuse to compile a program when a required cast is missing. – mafso Jan 05 '15 at 16:37
  • Consider editing your question explaining why certain questions aren't duplicates (and which part of your question aren't addressed by them) so it gets a chance to be reopened and get a real answer. – mafso Jan 05 '15 at 16:40
  • 1
    http://stackoverflow.com/questions/27785497/casting-a-pointer-what-is-the-difference-at-runtime – mafso Jan 06 '15 at 09:44
  • "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." I think this would answer the above statement: https://stackoverflow.com/q/19292224/15933960 –  Dec 30 '21 at 16:44

0 Answers0