saymaname
saymaname

Reputation: 39

Code of pointers

So I tried to do a code: the main get input three numbers and than send the pointers of those numbers to function which need to swap between of them until we had what we want. We want that in the end of the code\function the biggest number will be in num1, the second biggest number will be in num2, and the smallest number will be in num3. please help me with the pointers. the function prints some garbage value.

#include <stdio.h>
#include <stdlib.h>

void whichIsBigger(int* num1, int* num2, int* num3);
void swap(int* num1, int* num2);
void printsAnswer(int* num1, int* num2, int* num3);

int main(void)
{
    int num1, num2, num3;
    int* pnum1 = &num1;
    int* pnum2 = &num2;
    int* pnum3 = &num3;
    printf("Please enter three number\n");
    scanf("%d %d %d",&num1,&num2,&num3);
    whichIsBigger(pnum1, pnum2, pnum3);
    system("PAUSE");

    return 0;
}


/*
*/
void whichIsBigger(int* num1, int* num2, int* num3)
{
    if ((*num3 > *num1) && (*num3 > *num2))
    {
        if (*num1 > *num2)
        {
            swap(&num1, &num2);
        }
    }
    else if ((*num2>*num1) && (*num2>*num3))
    {
        swap(&num2, &num3);
        if (*num1 > *num2)
        {
            swap(&num1, &num2);
        }

    }
    else if ((*num1 > *num2) && (*num1 > *num3))
    {
        swap(&num1, &num3);
        if (*num1 > *num2)
        {
            swap(&num1, &num2);
        }

    }

    printsAnswer(&num1, &num2, &num3);

}


/*
*/
void swap(int* num1, int* num2)
{
    int temp = *num1;
    *num1 = *num2;
    *num2 = temp;
}

/*
*/
void printsAnswer(int* num1, int* num2, int* num3)
{
    printf("the biggest number is: %d\n", *num1);
    printf("the second biggest number is: %d\n", *num2);
    printf("the smallest number is: %d\n", *num3);
}

Upvotes: 1

Views: 99

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

The function can look the following way

void whichIsBigger( int *num1, int *num2, int *num3 )
{
    if ( *num1 < *num2 ) swap( num1, num2 );
    if ( *num2 < *num3 ) swap( num2, num3 );
    if ( *num1 < *num2 ) swap( num1, num2 );
}

And in main you can write

whichIsBigger( pnum1, pnum2, pnum3 );
printsAnswer( pnum1, pnum2, pnum3 );

As for your function then apart from the incorrect call of the swap it is in essense wrong because in general at least two numbers can be equal each other. In this case neither condition in the if statements will be equal to true. Consider for example when num1 is equal 1 and num2 and num3 are equal to 2.

Take into account that there is no sense to declare the parameters of the functions printAnswer as pointers. The function could be defined like

void printsAnswer( int num1, int num2, int num3 )
{
    printf( "the biggest number is: %d\n", num1 );
    printf( "the second biggest number is: %d\n", num2 );
    printf( "the smallest number is: %d\n", num3 );
}

Upvotes: 1

Joseph Courtright
Joseph Courtright

Reputation: 71

When you send the pointers to the swap function you add '&' which sends the address. Because void swap(int* num1, int* num2); takes pointers as input it already knows it wants addresses. So instead you are swapping around the address of the pointers.

When you call swap do not use the addresses

swap(num2, num3);

I believe that will fix your program.

Upvotes: 3

Related Questions