简体   繁体   English

数组的最大值

[英]Maximum value of an array

The program should find the maximum value of the array, but I'm facing an error I cannot fix.程序应该找到数组的最大值,但我遇到了一个我无法修复的错误。

I get the following error:我收到以下错误:

invalid conversion from 'int*' to 'int'.从“int*”到“int”的无效转换。

Replacing int by float works as expected.float替换int可以按预期工作。

#include<stdio.h>
void find_biggest(int, int, int *);
int main()
{
    int a[100], n, *biggest;
    int i;
    
    printf("Number of elements in array ");
    scanf("%d",&n);
    
    for(i=0;i<n;i++)
    {
        printf("\nEnter the values: ");
        scanf("%d",&a[i]);
    }
    
    find_biggest(a, n, &biggest); //invalid conversion  from 'int*' to 'int'
    
    printf("Biggest = %d",biggest);
    
    return 0;
}

void find_biggest(int a[], int n, int *biggest)
{
    int i;
    *biggest=a[0];
    for(i=1; i<n; i++)
    {
        if(a[i]>*biggest)
        {
            *biggest=a[i];
        }
    }
} 

You've got a mismatch between your function prototype:您的 function 原型不匹配:

void find_biggest(int, int, int *);

And definition:和定义:

void find_biggest(int a[], int n, int *biggest)

Since you're passing the array a as the first argument, the definition is correct and the prototype is incorrect.由于您将数组a作为第一个参数传递,因此定义正确且原型不正确。 Change it to:将其更改为:

void find_biggest(int [], int, int *);

Or:或者:

void find_biggest(int *, int, int *);

You're also passing the wrong type for the third argument.您还为第三个参数传递了错误的类型。 You define biggest as an int pointer and are passing its address, so the given parameter has type int ** when it expects int .您将biggest定义为int指针并传递其地址,因此给定参数的类型为int **当它需要int时。 So change type type of biggest to int .所以将biggest的类型类型更改为int

int a[100], n, biggest;

The word find is a word reserved in langage C,you must avoid it find这个词是语言C中的保留词,你必须避免它

#include<stdio.h>
int main()
{
   int a[100], n, *biggest;
   int i;

printf("Number of elements in array ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
    printf("\nEnter the values: ");
    scanf("%d",&a[i]);
}

biggest_array(a, n, &biggest); //invalid conversion  from 'int*' to 'int'

printf("Biggest = %d",biggest);

return 0;
}

void biggest_array(int a[], int n, int *biggest)
{
int i;
*biggest=a[0];
for(i=1; i<n; i++)
{
    if(a[i]>*biggest)
    {
        *biggest=a[i];
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM