Search code examples
cpointersfunction-declaration

What are pointers in C


I have this task:

Make corrections in the maxmin.c program so it compiles itself and works correctly (finds max and min out of 3 integer numbers and displays them on the screen). Do not change the structure of the program. In case of incorrect input, you must output n/a.

I've been experimenting with this code below for a couple of hours, but I still can't figure out what exactly is wrong here🤯

#include <stdio.h>

void maxmin(int prob1, int prob2, int prob3, int *max, int min);

/* Find a max & min probabilities */
int main()
{
    int x, y, z;
    scanf("%d %d %d", x, y, z);

    int max, min;

    maxmin(x, y, z, max, min);

    printf("%d %d", max, min);

    return 0;
}


/* This function should be kept !!! (Your AI) */
/* But errors & bugs should be fixed         */
void maxmin(int prob1, int prob2, int prob3, int *max, int min)
{
    *max = min = prob1;
    
    if(prob2 > max)
        max = prob2;
    if(prob2 < min)
        min = prob2;
    
    if(prob3 > max)
        max = prob3;
    if(prob3 < min)
        min = prob3;    
}

enter image description here


Solution

  • By default arguments are passed to functions by values. It means that functions dial with copies of values of argument expressions.

    Changing a copy of the value of an object within a function does not change the value of the original object.

    To change the original object in a function you need to pass it to the function by reference.

    In C passing by reference means passing an object indirectly through a pointer to it. So dereferencing the pointer within the function you get a direct access to the original object and can change its value.

    In the function maxmin you want to change the objects min and max declared in main

    int max, min;
    

    So the function needs to accept these objects by reference that is through pointers to them.

    It means that the function must be declared like

    void maxmin(int prob1, int prob2, int prob3, int *max, int *min);
    

    and called in main like

    maxmin(x, y, z, &max, &min);
    

    As it was already mentioned to access the original objects min and max you need to dereference the pointers.

    Thus the function definition will look like

    /* This function should be kept !!! (Your AI) */
    /* But errors & bugs should be fixed         */
    void maxmin( int prob1, int prob2, int prob3, int *max, int *min )
    {
        *max = *min = prob1;
        
        if ( prob2 > *max )
            *max = prob2;
        else if ( prob2 < *min )
            *min = prob2;
        
        if ( prob3 > *max )
            *max = prob3;
        else if ( prob3 < *min )
            *min = prob3;    
    }
    

    The function scanf also needs to accept its arguments by reference. So in main you have to write

    scanf("%d %d %d", &x, &y, &z);
    

    and in the format string to specify three conversion specifiers.

    Also bear in mind that according to your assignment you need

    In case of incorrect input, you must output n/a.

    It means that you need to check whether the call of scanf was successful that is whether all three values were inputted correctly.

    It can be done the following way

    int x, y, z;
    
    if ( scanf( "%d %d %d", &x, &y, &z ) == 3 )
    {
        int max, min;
    
        maxmin( x, y, z, &max, &min );
        //...
    }
    else
    {
        puts( "n/a" );
    }