Casting void pointers

前端 未结 4 1284
抹茶落季
抹茶落季 2020-12-03 10:31

I\'ve seen a lot of the following in older C code:

type_t *x = (type_t *) malloc(...);

What\'s the point of casting the pointer returned fr

4条回答
  •  失恋的感觉
    2020-12-03 11:37

    The problem here is not compatibility with any dialect of C. The problem is C++. In C++, a void pointer cannot be automatically converted to any other pointer type. So, without an explicit cast, this code would not compile with a C++ compiler.

提交回复
热议问题