通过参考来找到最小数组
我试图使用通过参考通过 来编写,从而找到了数组的最小值,同时仅给出一个数组的指针,而数组的长度作为一个参数到功能中。
我一直遇到这样的错误:
不允许使用不完整的类型(在主函数中) 从“ int”中分配“ int *'
这是我的代码:
void minimum(int *iArray[], int count)
{
int min = iArray[0];
for(int i = 1; i < count; i++)
{
if (min > iArray[i])
*iArray = min;
}
}
int main()
{
int numbers[] = {4, 6, 9, 5};
printf("%d", minimum(numbers, 4));
return 0;
}
您能帮助我如何做得更好吗?
I am trying to write a void Function using pass by reference , that finds the minimum of an Array while only giving a pointer of an Array and the length of the Array as a Parameter into the Function.
I keep getting errors like:
Incomplete type is not allowed (in the main function)
assignment to 'int *' from 'int' makes pointer from integer without a cast.
This is my code:
void minimum(int *iArray[], int count)
{
int min = iArray[0];
for(int i = 1; i < count; i++)
{
if (min > iArray[i])
*iArray = min;
}
}
int main()
{
int numbers[] = {4, 6, 9, 5};
printf("%d", minimum(numbers, 4));
return 0;
}
Can you help me out how i can do it better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
声明和函数的定义是不正确的。
在此功能调用中,
该数组被隐式转换为指针转换为其类型
int *
的第一个元素。 类型int *[]
。int ** 的
但是相应的函数参数具有由编译器调整为类型
>
int 的最小由类型int *
的指针初始化。同样,在找到最小元素的函数中更改源数组是一个坏主意。
并且由于函数具有返回类型
void
,因此函数的调用printf
无效,应声明并定义以下方式
,并且该函数的调用将查看或者
,可以声明和定义函数以下方式
,该功能的调用看起来像
The declaration and the definition of the function are incorrect.
In this function call
the array is implicitly converted to pointer to its first element of the type
int *
. But the corresponding function parameter has the typeint *[]
that is adjusted by the compiler to the typeint **
..Thus for example in this line
the object
min
of the typeint
is initialized by a pointer of the typeint *
.Also it is a bad idea to change the source array in a function that finds the minimal element.
And as the function has the return type
void
then the call of the functionprintf
is invalidThe function should be declared and defined the following way
And the call of the function will look like
Alternatively the function can be declared and defined the following way
And the call of the function will look like
数字
是指向数组的第一个元素的指针。因此,您的功能定义应该是: -或
注意:查找最小值的逻辑有一些错误,您应该返回
min
,以用printf
打印它。您的完整代码看起来像: -numbers
is pointer to first element of the array. Thus your function definition should be like :-or
Note: Your logic for finding minimum has some errors and you are supposed to return the
min
for it to be printed withprintf
. Your complete code will look like :-