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

后端 未结 1 398
野性不改
野性不改 2020-12-18 17:04

I\'m writing a program that calculates the greatest common denominator of two numbers, but i\'m getting problem with malloc function and pointers. Actually it\'

相关标签:
1条回答
  • 2020-12-18 17:18

    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.

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