Why does the compiler complain when I do not cast the result of malloc?

前端 未结 2 431
礼貌的吻别
礼貌的吻别 2021-01-15 09:47

I am inspecting code that does not require explicitly casting result of malloc call but whenever I attempt to do this, the compiler throws an error.

i.e.

<         


        
相关标签:
2条回答
  • 2021-01-15 10:19

    Did you remember to include the function prototype? For malloc(3), this is:

    #include <stdlib.h>
    
    0 讨论(0)
  • 2021-01-15 10:29

    This happens if you use C++ compiler instead of C compiler. As C++ requires explicit casting. The problem is not just with (un)casting malloc result, but any void pointer to other pointer.

    0 讨论(0)
提交回复
热议问题