0

Writing a program using the Sieve of Tratosthenes theory and I cant covert void to int.

Heres my code:

#include <stdio.h>
#include <stdlib.h>

#define limit 1000000 /*size of integers array*/
void *malloc(size_t size)

int main(){
    unsigned long long int i, j;
    int *primes;
    int z = 1;
    int times;
    int counter;

    printf_s("Enter the limit: ");
    scanf_s("%d\n", &times);

    do (counter++) {
        primes = malloc(sizeof(int) * limit);

        for (i = 2; i < limit; i++)
            primes[i] = 1;

        for (i = 2; i < limit; i++)
            if (primes[i])
                for (j = i; i * j < limit; j++)
                    primes[i * j] = 0;

        printf("\nPrime numbers in range 1 to 100 are: \n");
        for (i = 2; i < limit; i++)
            if (primes[i])
                printf("%d\n", i);
    } while (counter <= times)

    return 0;
}

There error occurs in this line:

primes = malloc(sizeof(int) * limit);

Can someone point me to what Im doing wrong? Am I not using the correct library or is the function with "malloc()" coded incorrectly? This code in a C++ compiler, but I did research and saw it can work in a C compiled program. Thanks

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
dreed
  • 11
  • 3
  • 2
    Why are you declaring this `void *malloc(size_t size)` (btw you forgot the `;` at the end)? Btw, please, show us exactly which error you're obtaining. – nbro Mar 05 '17 at 01:18
  • 2
    `primes = (int*)malloc(sizeof(int) * limit);` must be – RbMm Mar 05 '17 at 01:19
  • Should this be tagged C? If this is meant to be C++, you have more fundamental problems as this is a non-idiomatic and error prone way of doing this. If this is meant to be C then it should be tagged that way. Btw this is also related to your implicit conversion; that conversion is legal in C but not C++. Or, I guess, if your goal is to write code that is valid both C and C++, you should state that clearly as it's uncommon. – Nir Friedman Mar 05 '17 at 01:21
  • Did you read what the error say? [Do I cast the result of malloc?](http://stackoverflow.com/q/605845/995714) – phuclv Mar 05 '17 at 01:54

0 Answers0