问题描述:

I was trying to create a function using array and getting the greater value, but I'm failing and getting these messages, anyone may help me?

\Untitled4.c In function 'main':

25 27 \Untitled4.c [Warning] passing argument 1 of 'quatroMaior' makes

pointer from integer without a cast

3 5 \Untitled4.c [Note] expected 'int *' but argument is of type 'int'

#include <stdio.h>

int greater(int array[]) {

int i, greater;

for (i = 0; i < 4; i++) {

if(array[i] > greater) {

greater = array[i];

}

}

return greater;

}

int main(void) {

int j, v[4];

printf("Type four values\n");

for (j = 0; j < 4; j++) {

scanf("%d", &v[j]);

}

printf("%d", greater(v[4]));

system("pause");

return 0;

}

网友答案:

As Joseph said in the comments, you are passing an integer, not an array. You have to write greater(v) instead of greater(v[4]).

The next thing you need to do is initialize the greater variable to greater = 0.

#include <stdio.h>

int greater(int array[])
{
    int i, greater = 0;

    for (i = 0; i < 4; i++)
    {
        if (array[i] > greater)
        {
            greater = array[i];
        }
    }

    return greater;
}

int main (void)
{
    int j, v[4];

    printf("Type four values\n");
    for (j = 0; j < 4; j++)
    {
        scanf("%d", &v[j]);
    }

    printf("%d\n", greater(v));

    return 0;
}
网友答案:
#include <stdio.h>

int greater(int array[]) {
    int i, greater;
/* greater is not init so you have an Undefined behavior.
** You could init it with array[0] but remind that need that size of array is at least 1.*/

    for (i = 0; i < 4; i++) { // If you do start at 1 because 0 is already in greater.
        if (array[i] > greater) { // here greater is not init !
            greater = array[i]; 
        }
    }

    return greater;
}

int main(void) {

    int j, v[4];

    printf("Type four values\n");

    for (j = 0; j < 4; j++) {
        scanf("%d", &v[j]);
    }

    printf("%d", greater(v[4]));
/* you send v[4] but you array has only range from 0 to 3.
** And is not what greater function expect he expect a array so just v.*/

    system("pause");
    return 0;
}
相关阅读:
Top