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);
}
gcc -Wall your_file.cThe main issue is that you are passing pointers to pointers instead of pointers.