c++floating-pointvoid-pointerstypecasting-operator

typecasting of pointers in cpp


Can anybody tell me what's going on with the following code:

int compareX(const void* a, const void* b)  
 {  
    Point *p1 = (Point *)a, *p2 = (Point *)b;  
    return (p1->x - p2->x);  
 }

I have taken this code from geeks for geeks, the closest pair of points. can anyone explain to me the similar code in a much simpler and easy way with line by line explanation

also, I am unable to understand this piece of code as well:

float bruteForce(Point P[], int n)  
{  
    float min = FLT_MAX;
    for (int i = 0; i < n; ++i)  
        for (int j = i+1; j < n; ++j)  
            if (dist(P[i], P[j]) < min)  
                min = dist(P[i], P[j]);  
    return min;  
}  

What's the use of FLT_MAX here?


Solution

  • FLT_MAX is a constant defined here and the logic if used to find the Point with the lowest distance between all the pair of points in the array, the idea of setting the variable min with a FLT_MAX is to define a threshold limit value that can be exceeded by the 1st comparison of the 1st 2 Points...