传递参数使得指针来自整数

Bro*_*ens 7 c arrays pointers integer casting

我找不到我的问题.不断给我这些错误:

"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]"
Run Code Online (Sandbox Code Playgroud)

这是代码:

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 }
Run Code Online (Sandbox Code Playgroud)

Mik*_*ike 13

你的问题的答案是交换这个:

CountEvenNumbers(numbers[length], length);
Run Code Online (Sandbox Code Playgroud)

为了这

CountEvenNumbers(numbers, length);
Run Code Online (Sandbox Code Playgroud)

但是,如果您继续编码,您可能会发现无用的技能是解密warrning /错误消息:

"c:2:5:注意:预期'int*'但是参数的类型为'int'"
"c:28:1:警告:传递'CountEvenNumbers'的参数1使得整数指针没有强制转换[默认启用] ]"

那是什么意思呢?它声明在第28行(CountEvenNumbers( numbers[length] , length ); )它期望你进行参数1的转换,这意味着你传递了一些它没想到的东西.所以你知道第一个参数有问题.

这里的诀窍是另一行:expected 'int *' but argument is of type 'int'它说"我想要一个指向整数的指针,但你给了我一个整数".这就是你知道你传递错误类型的方式.

所以你应该问自己的是,论证1的类型是什么?你知道如果你想访问数组中你需要使用的元素[],(你在代码的第20行和第25行中这样做了),所以通过传递numbers[length]给你的函数,你试图传递一个元素1而不是像预期的那样完整的数组.

另一半是expected 'int *',为什么你的函数期望获得一个指向int的指针?那是因为在C中,当你传递一个(类型)数组时,它会衰减到指向(类型)的指针.

当然号码[长度] 1是不是一个真正的元件您的阵列,无论如何,它溢出它.


小智 0

真的,请阅读 C 教程。array[index]下标/索引数组,因此它产生index数组中的第 th 个元素。如果你想传递要对其自身进行操作的数组(好吧,而是指向其第一个元素的指针),那么只需写下它的名称:

CountEvenNumbers(numbers, length);
Run Code Online (Sandbox Code Playgroud)