Search code examples
ctypedefgcc-warningqsort

Why qsort cause error in gcc 6.3.0 by using typedef enum?


I'm working for Strongly Connected Component (SCC) algorithm.

So, I sorted vertices by increasing order using qsort function.

To use qsort, I made my own compare function and used typedef enum{false,true} bool.

VS2017 IDE compiles successfully of this but MinGW which has gcc 6.3.0 cause error like below.

enter image description here

My CreateSorted and qsort compare function are these codes.

// qsort compare function, descending order
bool cmp(const void *p1, const void *p2)
{
    VF* vf1 = (VF*)p1;
    VF* vf2 = (VF*)p2;
    return vf2->f - vf1->f;
}

// Create sorted vertices array of VF structure
// For DFS of decreasing finish time vertex
VF* CreateSorted(adjList* adj)
{
    VF *sorted_vertices = (VF*)malloc(sizeof(VF)*(adj->vertexNum+1));

    for (int i = 1; i <= adj->vertexNum; i++) {
        Node* current = adj[i].nodeList;
        sorted_vertices[i].v = current->v;
        sorted_vertices[i].f = current->f;
    }
    qsort(sorted_vertices+1, adj->vertexNum, sizeof(VF), cmp);
    return sorted_vertices;
}

What I really curious is the reason of error resulting from typedef enum{false, true} bool.


Solution

  • The fourth parameter to qsort should be a pointer to a function with the following prototype:

    int compar (const void* p1, const void* p2)

    The prototype of your function is:

    bool compar (const void* p1, const void* p2)