# % illegal error?

• 09-09-2008
alyeska
% illegal error?
Hi everyone,
I am having this error:
Code:

`error C2296: '%' : illegal, left operand has type 'int *'`
What does this mean?

Code:

```//Write a function countEven(int*, int) which receives //an integer array and its size, and returns the number //of even numbers in the array. #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int countEven(int* array, int n) {         for (int i = 0; i < n; ++i)         {                 if (array % 2 == 0)  // <----ERROR!                         cout << *array << " ";                 else                         continue;         }         return *array; } int main() {         int el = 0;         cout << "Enter the number of array elements: ";         cin >> el;         int* Array = new int[el]; //dynamic memory allocated                 cout << "Enter your array values: " << endl;         for (int i = 0; i < el; ++i)         {                 cout << "[" << i << "]: ";                 cin >> *(Array + i);         }         cout << "Your array values are: {";         for (int i = 0; i < el; ++i)         {                 cout << *(Array + i) << ",";         }         cout << "}" << endl;         cout << "Your even number elements are: {";         countEven(Array, el);         cout << "}" << endl;         delete Array;         Array = 0; }```
Thanks!;)
• 09-09-2008
laserlight
Quote:

What does this mean?
It means that you are trying to compute the remainder of dividing an address by 2. You actually want to compute the remainder of dividing the integer at that address by 2 (or otherwise determine if the integer is even or odd).
• 09-09-2008
alyeska
I modified it, however, it only outputs the first even element, and it outputs it three times. Why do you think that is?
Thanks..

Code:

```int countEven(int* array, int n) {         for (int i = 0; i < n; ++i)         {                 if (array[i] % 2 == 0)                         cout << *array << " ";                 else                         continue;         }         return *array; } int main() {         int el = 0;         cout << "Enter the number of array elements: ";         cin >> el;         int* Array = new int[el]; //dynamic memory allocated                 cout << "Enter your array values: " << endl;         for (int i = 0; i < el; ++i)         {                 cout << "[" << i << "]: ";                 cin >> *(Array + i);         }         cout << "Your array values are: {";         for (int i = 0; i < el; ++i)         {                 cout << *(Array + i) << ",";         }         cout << "}" << endl;         cout << "Your even number elements are: {";         countEven(Array, el);         cout << "}" << endl;         delete Array;         Array = 0; }```
• 09-09-2008
matsp
Code:

```                if (array[i] % 2 == 0)                         cout << *array << " ";```
The error is in the above. Can you spot it?

--
Mats
• 09-09-2008
laserlight
Quote:

I modified it, however, it only outputs the first even element, and it outputs it three times. Why do you think that is?
Notice that you are printing *array instead of array[i]. Note also that you are returning *array.

By the way, the else continue is redundant. Oh, and remember to match new[] with delete[], not delete.
• 09-09-2008
matsp
Code:

```int countEven(int* array, int n) { ...         return *array; }```
Is the *array actually the number of even numbers?

--
Mats
• 09-09-2008
alyeska
Thanks, laserlight and matsp..
I am still a little bit confused when it comes to pointers and actually, references too. Thanks for explaining the errors, laserlight. You're a fine teacher! Thanks!
• 09-09-2008
alyeska
Hi matsp,
Actually no, I got it all wrong, it was supposed to be the even elements. Pointer syntax still confuse me, thanks for your help..