Conductive argument makes a pointer of an integer

I can not find my problem. keeps giving me these errors:

"c:2:5: note: expected 'int *' but argument is of type 'int'" "c:28:1: warning: passing argument 1 of 'CountEvenNumbers' makes pointer from integer without a cast [enabled by default]" 

Here is the code:

 1 #include <stdio.h> 2 int CountEvenNumbers(int numbers[], int length); 3 int main(void) 4 { 5 int length; 6 int X;int Z; int Y; int W; 7 X=0;Y=0;Z=0;W=0; 8 printf("Enter list length\n"); 9 scanf("%d",&length); 10 int numbers[length]; 11 12 if (length<=0) 13 . {printf("sorry too low of a value\n"); 14 . . return 0;} 15 else 16 . { 17 . printf("Now, enter %d integers\n",length); 18 . for (X=0;X<length;X++) 19 . . {scanf("%d",&Y);//X is position in array, Y is value. 20 . . numbers[X]=Y; 21 . . } 22 . printf("The list reads in as follows:\n"); 23 . for (W=0;W<length;W++) 24 . . {Z=numbers[W]; 25 . . printf("%d ",Z);} 26 . printf("\n"); 27 . } 28 CountEvenNumbers( numbers[length] , length ); 29 return 0; 30 } 31 32 int CountEvenNumbers(int numbers[], int length) 33 { 34 . int odd_count;int even_count;int P;int Q; 35 . Q=0; odd_count=0;even_count=0; 36 . for (P=0;P<length;P++) 37 . . if (numbers[Q]==0) 38 . . . {even_count++; 39 . . . Q++;} 40 . . else if ((numbers[Q]%2)!=0) 41 . . . {odd_count++; 42 . . . Q++;} 43 . . else 44 . . . {even_count++; 45 . . . Q++;} 46 . printf("There are %d even numbers in the series\n",even_count); 47 . return 0; 48 } 
+6
source share
5 answers

The answer to your question is to replace this:

 CountEvenNumbers(numbers[length], length); 

for this

 CountEvenNumbers(numbers, length); 

However, if you continue coding, a skill that may prove invaluable is to decrypt error / error messages:

"c: 2: 5: note: expected 'int *', but the argument is of type 'int'"
"c: 28: 1: warning: passing argument 1 of" CountEvenNumbers "makes a pointer to an integer without cast [enabled by default]"

So what does that mean? It states that on line 28 ( CountEvenNumbers( numbers[length] , length ); ) it was expected that you would throw an argument 1, that is, you passed it what he did not expect. So, you know that something is wrong with the first argument.

The trick here is another line: expected 'int *' but argument is of type 'int' He says: "I need a pointer to an integer, but you gave me just an integer." This is how you know that you are going through the wrong type.

So you have to ask yourself what type of argument is 1? You know if you want to access an element inside an array, you need to use [] , (you did this on lines 20 and 25 of your code), therefore, by passing numbers[length] your function, your attempt to pass it one element 1 instead the full array that he expects.

The other half of this is expected 'int *' , why does your function expect to get a pointer to an int? Good thing, since in C, when you pass an array (type), it decays to a pointer to (type).

1, of course, the number [length] is not really an element in your array, it overflows it.

+11
source

On line 28, you are trying to pass an integer at the index "length" of numbers. You should just pass in the numbers, so something like CountEvenNumbers(numbers, length);

+3
source

Read the C tutorial, really. array[index] indexes / indexes the array, and thus it gives the index th element in the array. If you want to pass an array that will work on its own (well, rather, a pointer to its first element), just write its name:

 CountEvenNumbers(numbers, length); 
+1
source

Try it.

 #include <stdio.h> #include <stdlib.h> int CountEvenNumbers(int numbers[], int length); int main(void) { int length; int X;int Z; int Y; int W; X=0;Y=0;Z=0;W=0; printf("Enter list length\n"); scanf("%d",&length); int *numbers = (int*) calloc(length, sizeof(int)); //*************** if (length<=0) {printf("sorry too low of a value\n"); return 0;} else { printf("Now, enter %d integers\n",length); for (X=0;X<length;X++) {scanf("%d",&Y);//X is position in array, Y is value. numbers[X]=Y; } printf("The list reads in as follows:\n"); for (W=0;W<length;W++) {Z=numbers[W]; printf("%d ",Z);} printf("\n"); } CountEvenNumbers( numbers , length ); //************** free (numbers); return 0; } int CountEvenNumbers(int numbers[], int length) { int odd_count;int even_count;int P;int Q; Q=0; odd_count=0;even_count=0; for (P=0;P<length;P++) if (numbers[Q]==0) {even_count++; Q++;} else if ((numbers[Q]%2)!=0) {odd_count++; Q++;} else {even_count++; Q++;} printf("There are %d even numbers in the series\n",even_count); return 0; } 
+1
source

check "& variable" in function

-1
source

All Articles