-1

The following code runs fine while I'm using it in my Code blocks application. However, when solving the "Even Odds" problem in Codeforces by submitting the same code, it gives me a Compilation error.

Error:invalid conversion from 'void*' to 'long long int*' [-fpermissive]

I'm quite new to C programming. So, plz help me how I can fix this code of mine.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
long long *arr, n, k, i, n1, n2, m;
scanf("%I64d%I64d", &n, &k);
arr=malloc(sizeof(long long)*n);
    n1=1;n2=2;
for(i=0;i<=n/2&&n1<=n;i++){
    arr[i]=n1;
    n1+=2;
    }
if(n%2!=0) m=n/2+1;
else m=n/2;
for(i=m;i<=n&&n2<=n;i++){
    arr[i]=n2;
    n2+=2;
    }
printf("%I64d", arr[k-1]);
free(arr);

return 0;
}
mpromonet
  • 11,326
  • 43
  • 62
  • 91

1 Answers1

1

In Codeblocks, you must be compiling it as a c file. But in Codeforces, the code is being compiled as cpp.

arr=malloc(sizeof(long long)*n);

When you compile that line in C, the malloc will throw a pointer of void* which will be automatically converted into long long*

But in the case of cpp, explicit conversion should be performed. To run the code in cpp:

arr = (long long*)malloc(sizeof(long long) * n);

That will do the job. Read this article for clarification.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53