1

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);
}
3
  • 2
    Try compiling it with warnings turned on and you'll learn something valuable :) For instance gcc -Wall your_file.c The main issue is that you are passing pointers to pointers instead of pointers. Commented Mar 15, 2016 at 20:14
  • 3
    Why are you using swap as swap(&num1,&num2) and not swap(num1,num2) ? Its already a pointer Commented Mar 15, 2016 at 20:14
  • On a side note the whichIsBigger function seems to give the opposite result (showing the smallest number as largest etc) Commented Mar 15, 2016 at 20:18

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.