这个简单的程序为什么出错?

来源:百度知道 编辑:UC知道 时间:2024/06/15 07:29:11
int *a;
a = malloc(100);

编译时错误,怎么回事
error C2440: '=' : cannot convert from 'void *' to 'int *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast

int *a;
a = (int *)malloc(100);
malloc返回void*类型,需要转换成int*

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

你的那个自定义函数是无返回类型的,会出现这个错误了,你的那个自定义函数要有一个整型的返回值

int *a;
a = (int *)malloc(100);