invalid conversion from *void to *int [-fpermissive] using malloc(sizeof())

痞子三分冷 提交于 2019-11-29 08:58:42

You need to cast:

int *a = (int*)malloc(num * sizeof(int));

Because there's no implicit conversion from void* to type * in C++.

Note that this cast is not required in C and could potentially be dangerous to do so in C.

Except for #include <iostream>, nothing in your code is C++. So remove it and compile it with a C compiler and you wouldn't need this cast.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!