Pointer + Array + Function Problem~~

This is a discussion on Pointer + Array + Function Problem~~ within the C Programming forums, part of the General Programming Boards category; Code:
Hi... i just start to learnt about pointer + function with array... this my toturial question.. now i try ...

Pointer + Array + Function Problem~~

Code:

Hi... i just start to learnt about pointer + function with array... this my toturial question.. now i try to do it..?
Write a function countEvent(int *, int) which receives an integer array and its size, and returns the number of even numbers in the array.
#include<stdio.h>
int countEvent(int *ptr, int size)
{
int i, even_cnt = 0;
for(i = 0; i<=size; i++)
{
if(*(ptr+1)%2 == 0)
even_cnt++;
}
return even_cnt;
}
int main ()
{
int array[10] = {1,2,3,4,5,6,7,8,9,10};
int even, count = 10;
even = countEvent(&array,count);
printf("%d\n", even);
return 0;
}
this is my program... it should be giving me answer 5 rather than 11; but i get the output is 11?? what happen with my program..
thank you very much~~

2) You are not moving your pointer along the array *(ptr+1) always points to the same element, perhaps you wanted *(ptr + i) in that spot.

3) You are overrunning your array's bounds... in C arrays start at 0 and go to size-1, thus an array of 10 elements has valid indexes of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 .... so your loops have to stop one short of the size... for (1 = 0; i < size; i++)

4) Although you may not have learned about bitwise operations yet, a much easier way of checking for odd/even integers is to and (&) it with 1 as in y = x & 1; For even numbers y = 0, for odd numbers y = 1.

5) When calling your function from main you do not need the & (address of) operator infront of the array name.

2) You are not moving your pointer along the array *(ptr+1) always points to the same element, perhaps you wanted *(ptr + i) in that spot.

3) You are overrunning your array's bounds... in C arrays start at 0 and go to size-1, thus an array of 10 elements has valid indexes of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 .... so your loops have to stop one short of the size... for (1 = 0; i < size; i++)

4) Although you may not have learned about bitwise operations yet, a much easier way of checking for odd/even integers is to and (&) it with 1 as in y = x & 1; For even numbers y = 0, for odd numbers y = 1.

5) When calling your function from main you do not need the & (address of) operator infront of the array name.

6) The correct form of main() is int main (void).

it seem i got lots of mistake in C languages le.. thanks for your advice... i'll be work hard to get 'A' in the subject... thank you!