通过参考来找到最小数组

发布于 2025-02-02 16:16:56 字数 570 浏览 3 评论 0原文

我试图使用通过参考通过 来编写,从而找到了数组的最小值,同时仅给出一个数组的指针,而数组的长度作为一个参数到功能中。

我一直遇到这样的错误:

不允许使用不完整的类型(在主函数中) 从“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兮子 2025-02-09 16:16:56

声明和函数的定义是不正确的。

在此功能调用中,

minimum(numbers, 4)

该数组被隐式转换为指针转换为其类型int *的第一个元素。 类型int *[]

int ** 的

int min = iArray[0];

但是相应的函数参数具有由编译器调整为类型> int 的最小由类型int *的指针初始化。

同样,在找到最小元素的函数中更改源数组是一个坏主意。

并且由于函数具有返回类型void,因此函数的调用printf无效,

printf("%d", minimum(numbers, 4));

应声明并定义以下方式

int * minimum( const int *a, size_t n )
{
    const int *min = a;

    for ( size_t i = 1; i < n; i++ )
    {
        if ( a[i] < *min ) min = a + i;
    }

    return ( int * )min;
}

,并且该函数的调用将查看或者

int *min = minimum(numbers, 4);

printf( "The minimum value is %d at position %tu.\n", *min, min - numbers );

,可以声明和定义函数以下方式

size_t minimum( const int *a, size_t n )
{
    size_t min = a;

    for ( size_t i = 1; i < n; i++ )
    {
        if ( a[i] < a[min] ) min = i;
    }

    return min;
}

,该功能的调用看起来像

size_t min = minimum(numbers, 4);

printf( "The minimum value is %d at position %zu.\n", numbers[min], min );

The declaration and the definition of the function are incorrect.

In this function call

minimum(numbers, 4)

the array is implicitly converted to pointer to its first element of the type int *. But the corresponding function parameter has the type int *[] that is adjusted by the compiler to the type int **..

Thus for example in this line

int min = iArray[0];

the object min of the type int is initialized by a pointer of the type int *.

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 function printf is invalid

printf("%d", minimum(numbers, 4));

The function should be declared and defined the following way

int * minimum( const int *a, size_t n )
{
    const int *min = a;

    for ( size_t i = 1; i < n; i++ )
    {
        if ( a[i] < *min ) min = a + i;
    }

    return ( int * )min;
}

And the call of the function will look like

int *min = minimum(numbers, 4);

printf( "The minimum value is %d at position %tu.\n", *min, min - numbers );

Alternatively the function can be declared and defined the following way

size_t minimum( const int *a, size_t n )
{
    size_t min = a;

    for ( size_t i = 1; i < n; i++ )
    {
        if ( a[i] < a[min] ) min = i;
    }

    return min;
}

And the call of the function will look like

size_t min = minimum(numbers, 4);

printf( "The minimum value is %d at position %zu.\n", numbers[min], min );
我们的影子 2025-02-09 16:16:56

数字是指向数组的第一个元素的指针。因此,您的功能定义应该是: -

int minimum(int *iArray, int count)

int minimum(int iArray[], int count)

注意:查找最小值的逻辑有一些错误,您应该返回min,以用printf打印它。您的完整代码看起来像: -

#include <stdio.h>

int minimum(int *iArray, int count)
{

    int min = iArray[0];
    for(int i = 1; i < count; i++)
    {
        if (min > iArray[i])
            min = iArray[i];

    }
    return min;
}


int main()
{
    int numbers[] = {4, 6, 9, 5};

    printf("%d", minimum(numbers, 4));

    return 0;
}

numbers is pointer to first element of the array. Thus your function definition should be like :-

int minimum(int *iArray, int count)

or

int minimum(int iArray[], int count)

Note: Your logic for finding minimum has some errors and you are supposed to return the min for it to be printed with printf. Your complete code will look like :-

#include <stdio.h>

int minimum(int *iArray, int count)
{

    int min = iArray[0];
    for(int i = 1; i < count; i++)
    {
        if (min > iArray[i])
            min = iArray[i];

    }
    return min;
}


int main()
{
    int numbers[] = {4, 6, 9, 5};

    printf("%d", minimum(numbers, 4));

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文