The main problem with your code is that the arrays should not be passed using the address operator (&). So if you wanted to pass the array to your countEven
function, you'd use the following
countEven(numbers);
This is mainly because the name of the array is a pointer to its first element.
The second problem is that you've forgotten to put the j in your function's return statement.
Last but not least, the size
and j
variables inside main
are not used, so you can get rid of them.
Here's how I'd write the above piece of code
#include <stdio.h>
int countEven (int *numbers)
{
int even[7];
int i=0, j=0;
for(; i < 7; ++i)
if(!(numbers[i] % 2))
even[j++]=numbers[i];
return j;
}
int main (void)
{
int numbers[] = {2, 5, 10, 16, 31, 17, 30};
countEven(numbers);
printf ("%d\n", countEven(numbers));
return 0;
}