今天在混编C和c++时遇到了这样的错误,是个小错误哦,贴出来不怕大家笑,希望可以对我这样的初学者有所帮助,解决方法,就是因为在C语言中可以把空指针付给一个非空直接而不用去详细指明类型,而C++中是强类型语言所以要声明合适的转换类型
ANSI C allowed conversion from a void pointer to a non-void-pointer without explicitly casting
In C you can convert void* to non-void* without witing the conversion operator,
but in C++ you can't. For example, this code is OK in C:
but
is'll cause the same error as you got in C++ (malloc returns void*, and is
forced to be assigned to int*). In C++ you should write (not as C++'sish at it
seems
):
just defined the type that correspond to your function's declaration using
typedef.
Code:
int *arr=malloc(16);

Code:
int *arr=(int*)malloc(16);