# Thread: Pointer + Array + Function Problem~~

1. ## 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. Code:
`for(i = 0; i<=size; i++)`
What is the value of size and how many times will you go through the loop?

Code:
`if(*(ptr+1)%2 == 0)`
What is that line doing? Does ptr ever change? Where's the use of your loop control variable, i?

3. OK... a few things....

1) Learn to indent your code and leave a blank line or 2 between code sections to make it more readable ....
Code:
```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;
}```
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).

4. Originally Posted by CommonTater
OK... a few things....

1) Learn to indent your code and leave a blank line or 2 between code sections to make it more readable ....
Code:
```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;
}```
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!

5. Originally Posted by Vinsento
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!
We learn from our mistakes...

Conversely... if we never discover our mistakes we will keep right on making them.