This is the code i was trying to compile for finding primitive root…but it gives me the error as.
int isPrimitive (int q, int a) {
int i,z;
double k,s;
s=1;
i=0;
double *factors=malloc(sizeof(double)*q-2);
while (s>0 && i<q-2) {
k=pow(a,i);
s=k-(q*floor(k/q));
for (z=0;z<(sizeof(*factors)/sizeof(factors[0]));z++) {
if (factors[z]==s) {
return 0;
}
}
factors[i]=s;
i++;
}
here in the line double *factors=malloc(sizeof(double)*q-2);
// error invalid conversion from void* to double* comes.
The problem is almost certainly using a C++ compiler on C code. In C
void*can be converted to any pointer type without a cast, while this is not true in C++, hence the error message. There are basically two solutions: use a C compiler (e.g.gccrather thang++), or cast the result of malloc.The latter can be done by:
But if you are actually writing C++, then using the
new []syntax is better:(Note that this requires using
delete[] factorsinstead offree(factors))