Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

c - passing argument makes pointer from integer

I can't 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
");
9 scanf("%d",&length);
10 int numbers[length];
11 
12 if (length<=0)
13 .   {printf("sorry too low of a value
");
14 .   .   return 0;}
15 else
16 .   {
17 .   printf("Now, enter %d integers
",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:
");
23 .   for (W=0;W<length;W++)
24 .   .   {Z=numbers[W];
25 .   .   printf("%d ",Z);}
26 .   printf("
");
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
",even_count);
47 .   return 0;
48 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The answer to your question is to swap this:

CountEvenNumbers(numbers[length], length);

for this

CountEvenNumbers(numbers, length);

However, if you continue with coding, a skill you might find invaluable is deciphering warrning/error messages:

"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]"

So what does that mean? It states that on line 28 (CountEvenNumbers( numbers[length] , length ); ) it expected you to make a cast of argument 1, meaning you passed it something that it did not expect. So you know something is wrong with the first argument.

The trick here is the other line: expected 'int *' but argument is of type 'int' It's saying "I wanted a pointer to an integer, but you gave me just an integer". That's how you know you're passing the wrong type.

So what you should be asking yourself is, what type is argument 1? You know if you want to access an element inside the array you need to use the []'s, (you did so on lines 20 and 25 of your code), so by passing numbers[length] to your function, your trying to pass it a single element1 instead of a full array like it expects.

The other half of this is expected 'int *', why would your function expect to get a pointer to an int? Well that's because in C, when you pass an array of (type) it decays to a pointer to (type).

1 of course numbers[length] isn't really an element in your array anyway, it overflows it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...