You cannot determine the number of primes by computing sizeof(num_primes)
: that's a compile-time constant, it does not change with the value that you put in num_primes
. It looks like your system uses four bytes for an int
, so iterating from zero to sizeof+1, inclusive, covers indexes 0, 1, 2, 3, 4, and 5, which is consistent with your observation that six numbers get printed.
If SieveEratosthenes
returns the number of primes that it found, your loop should iterate from zero to that number, not to sizeof(num_primes)
:
for (i = 0; i != num_primes ; i++){
printf("%d", primes[i]);
}
Note that I changed *primes++
to primes[i]
. This is to help you free primes
when you are done, and avoid a memory leak.