If the function that you call must decide the actual number of elements that it needs to allocate, you should pass a pointer to the allocated length along with the rest of the parameters, like this:
size_t actual_length;
int *arr = primes(100, &actual_length);
if (arr == NULL) {
... // Report an error
}
for (size_t i = 0 ; i != actual_length ; i++) {
printf("%d\n", array[i]);
}
The primes
would then look like this:
int *primes(int count, size_t *actual_length) {
size_t primes_needed = ...
int *res = malloc(sizeof(*res)*primes_needed);
*actual_length = primes_needed;
// Do calculations, perhaps some reallocs
// Don't forget to reassign *actual_length = ... on realloc
...
return res;
}