[Solved] Terminal showing “segmentation fault(core dumped)” in C, Please check what is wrong in my code?


Your problem is clear here: max, mid and and min are uninitialized. Try:

void swapy(double *x, double *y, double *z)
{

     double max; 
     double mid; 
     double min;   

     if( (*x>*y) && (*x>*z)) {

        max = *x;
     }
     else if((*y>*x) && (*y>*z)) {

        max = *y;
     }
     else {

        max = *z;
     }



     if((*x<*y) && (*x<*z)) {

        min = *x;
     }
     else if((*y<*x) && (*y<*z)) {

        min = *y;
     }
     else {

        min = *z;
     }

    if((*x != max) && (*x != min)) {

        mid = *x;
     }
     else if((*y != max) && (*y != min)) {

        mid = *y;
     }
     else {

        mid = *z;
     }

     *x = max;
     *y = min;
     *z = mid;
}

you can notice I added brackets in your if conditions. This is a good habit to get, else it could lead to unwanted behavior sometimes.

0

solved Terminal showing “segmentation fault(core dumped)” in C, Please check what is wrong in my code?