Is it a better practice to typecast the pointer returned by malloc?

后端 未结 7 2200
闹比i
闹比i 2020-12-10 19:30

For the C code below, compare the defintions of the int pointers a and b;

#include 
#include 

int main()
{
  int *a=malloc(s         


        
7条回答
  •  悲哀的现实
    2020-12-10 20:08

    Is it better in any way to typecast the pointer of type void, returned by the malloc function?

    Yes. It is clearer in meaning. It's the same reason I'd write

    a = b + (c * d);
    

    Now, I know the "()"s are not needed here, due to the rules of precedence of arithmetic operators, but they help me (and others) clearly see my intent.

    $.02, etc. :)

    -k

提交回复
热议问题