C语言 传递参数使指针从整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15162665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
passing argument makes pointer from integer
提问by Bronson Stephens
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\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 }
回答by Mike
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]"
"c:2:5: 注意: 预期为 'int *' 但参数类型为 'int'"
"c:28:1: 警告: 传递 'CountEvenNumbers' 的参数 1 使指针来自整数而不进行强制转换 [默认启用]”
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.
那是什么意思呢?它指出,在第 28 ( CountEvenNumbers( numbers[length] , length );
) 行,它希望您对参数 1 进行转换,这意味着您传递了它没有预料到的东西。所以你知道第一个参数有问题。
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.
这里的技巧是另一行:expected 'int *' but argument is of type 'int'它说“我想要一个指向整数的指针,但你只给了我一个整数”。这就是你如何知道你传递了错误的类型。
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 element1instead of a full array like it expects.
所以你应该问自己,参数 1 是什么类型?您知道如果要访问数组中的元素,则需要使用[]'s,(您在代码的第 20 行和第 25 行这样做了),因此通过传递numbers[length]给您的函数,您尝试将单个元素传递给它1而不是它所期望的完整数组。
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 decaysto a pointer to (type).
另一半是expected 'int *',为什么你的函数期望得到一个指向 int 的指针?那是因为在 C 中,当你传递一个(类型)数组时,它会衰减到一个指向(类型)的指针。
1 of course numbers[length] isn't really an element inyour array anyway, it overflows it.
当然号码[长度] 1是不是一个真正的元件在您的阵列,无论如何,它溢出它。
回答by Ken
On line 28, you're trying to pass the integer at index "length" of numbers. You should just pass numbers itself, so something like CountEvenNumbers(numbers, length);
在第 28 行,您试图在数字的索引“长度”处传递整数。你应该只传递数字本身,所以像CountEvenNumbers(numbers, length);
回答by Anastacio Gianareas
Try this.
尝试这个。
#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;
}
回答by Anastacio Gianareas
Read a C tutorial, really. array[index]subscripts/indexes the array, and thus it yields the indexth element in the array. If you want to pass the array to be operated on itself (well, rather a pointer to its first element), then simply write its name:
阅读 C 教程,真的。array[index]下标/索引数组,因此它产生index数组中的第 th 个元素。如果你想传递要对其自身进行操作的数组(好吧,而不是指向它的第一个元素的指针),那么只需写下它的名字:
CountEvenNumbers(numbers, length);

