使用 c-lib 函数 qsort 时如何删除“不兼容的函数指针类型”警告

问题描述

我正在尝试使用 c-lib 函数 qsort() 对 long 数组进行排序。我是这样做的:

import tkinter  as tk
from tkinter import ttk
from tkinter import *

class ABCApp(tk.Tk):

    def __init__(self,*args,**kwargs):

        tk.Tk.__init__(self,**kwargs)

        self.geometry("1500x750")

        main_frame = tk.Frame(self)
        main_frame.pack(side = 'top',fill = 'both',expand ='True')

        main_frame.grid_rowconfigure(0,weight=1)
        main_frame.grid_columnconfigure(0,weight=1)

        bg = PhotoImage(file="images\\bg.png")
        label_bgImage = Label(self,image=bg)
        label_bgImage.place(x=0,y=0)

        self.frames = {}

        for F in (HomePage,PageOne,PageTwo):
            frame = F(main_frame,self)
            self.frames[F] = frame
            frame.grid(row=0,column=0,sticky='nsew')

        self.show_frame(HomePage)

    def show_frame(self,container):

        frame = self.frames[container]
        frame.tkraise()


class HomePage(tk.Frame):

    def __init__(self,parent,controller):

        tk.Frame.__init__(self,parent)

        label = ttk.Label(self,text='Home Page',font =("Helvetica",20))
        label.pack(padx=10,pady=10)

        button1 = ttk.Button(self,text = "Page 1",command = lambda: controller.show_frame(PageOne))
        button1.pack()

        button6 = ttk.Button(self,text="Page 2",command=lambda: controller.show_frame(PageTwo))
        button6.pack()

class PageOne(tk.Frame):

    def __init__(self,controller):
        tk.Frame.__init__(self,parent)

        label1 = ttk.Label(self,text='Page 1',font=("Helvetica",20))
        label1.pack(padx=10,pady=10)

        button2 = ttk.Button(self,text="Back",command=lambda: controller.show_frame(HomePage))
        button2.pack()

        button5 = ttk.Button(self,command=lambda: controller.show_frame(PageTwo))
        button5.pack()

class PageTwo(tk.Frame):

    def __init__(self,parent)

        label2 = ttk.Label(self,text='Page 2',20))
        label2.pack(padx=10,pady=10)

        button3 = ttk.Button(self,command=lambda: controller.show_frame(HomePage))
        button3.pack()

        button4 = ttk.Button(self,text="Page 1",command=lambda: controller.show_frame(PageOne))
        button4.pack()

app = ABCApp()
app.mainloop()

代码运行良好,但 clang 产生以下警告

int
compar(long *e1,long *e2)   // for decreasing order
{
   if (*e1 > *e2) return -1;
   if (*e1 < *e2) return 1;
   return 0;
}

void
c_lib_quick_sort(long *a,long n)
{
   qsort(a,n,sizeof(long),compar);
}

有人可以告诉我如何消除警告吗?警告指的是我以前从未见过的 (* _Nonnull) 之类的东西。

我15年前写过类似的代码,但当时的gcc并没有产生任何警告。较新的编译器对类型更加严格。

解决方法

警告表明预期的函数签名是什么

passing argument to parameter '__compar' here
            int (* _Nonnull __compar)(const void *,const void *));

因此相应地更改它应该有助于解决警告:

int
compar(const void *p1,const void *p2)   // for decreasing order
{
    const long* e1 = (const long*)p1;
    const long* e2 = (const long*)p2;
    if (*e1 > *e2) return -1;
    if (*e1 < *e2) return 1;
    return 0;
}