简体   繁体   中英

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.

在此处输入图片说明

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM