Cleansy Cleansy - 1 month ago 18
C Question

Procedure and pointers C

im trying to make a procedure to find a maximum numbers out of 3, which i will assign later to another variable pointer in main program

int Maximum (int *a, int *b, int *c)
{

if (*a>=*b)
{
if (*a<*c)
{*a=*c;}
}
else
{
*a=*b;
if (*a<*c)
{*a=*c;}
}
return a;
}


compiler giving me error that "expected 'int*' but argument is of type 'int' "
so i changes that part to

int * Maximum (int *a, int *b, int *c)


but error sentence still same ?

Answer

First off, your original function is designed to return an int and you're returning a, which is an int pointer. Either return the int (*a), or change the function so it returns an int *.

However, based on your actual message:

expected 'int*' but argument is of type 'int'

and the fact you state you've made that change and still have the problem (note the word "argument" in the message), this indicates that the problem lies not with the return value but with the way you're passing the arguments to it.

You are actually passing int where the function expects int * and that's probably caused by you using something like:

int one = 3;
int two = 7;
int three = 42;
int maxOfThree = Maximum (one, two, three);

If you wanted to pass pointers to the function, you would need:

Maximum (&one, &two, &three);

(passing the pointers to the variables, not the values of them).

A better solution (more readable and with correct data types both going in to, and coming out of, the function) to your problem would probably be:

int Maximum (int a, int b, int c) {
    if ((a > b) && (a > c))
        return a;
    if (b > c)
        return b;
    return c;
}
Comments