Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 解释qsort库中使用的函数的typedef_C_Pointers_Function Pointers_Typedef_Qsort - Fatal编程技术网

C 解释qsort库中使用的函数的typedef

C 解释qsort库中使用的函数的typedef,c,pointers,function-pointers,typedef,qsort,C,Pointers,Function Pointers,Typedef,Qsort,我正在使用qsort库函数对结构元素数组进行排序,在Internet上搜索时发现了一个资源:@support.microsoft 我知道qsort函数需要由泛型指针进行类型转换 但是,我无法获得以下信息: typedef int (*compfn) (const void*, const void*); 已声明的及其后续调用: qsort((void *) &array, // Beginning address of array 10,

我正在使用qsort库函数对结构元素数组进行排序,在Internet上搜索时发现了一个资源:@support.microsoft

我知道qsort函数需要由泛型指针进行类型转换

但是,我无法获得以下信息:

typedef int (*compfn) (const void*, const void*);
已声明的及其后续调用:

qsort((void *) &array,              // Beginning address of array
      10,                           // Number of elements in array
      sizeof(struct animal),        // Size of each element
      (compfn)compare               // Pointer to compare function
 );
  • typedef
    的表现如何,我的意思是我们到底输入了什么
    int(*compfn)
    还是
    int(compfn)
  • 如果是前者,那么调用不应该是
    (*compfn)

    语法:

    typedef  int (*compfn)  (const void*, const void*);
      ^      ^       ^            ^          ^
      | return type  |               arguments type
      |             new type name 
      defining new type
    
    compfn
    是由
    typedef
    关键字定义的新用户定义的
    type

    因此,您的typedefded
    int(*)(const void*,const void*)
    comfn
    使用上述语法

    声明:

     compfn  fun; // same as: int (*fun)  (const void*, const void*);
    
    意思是
    fun
    是一个函数指针,它接受两个
    const void*
    类型的参数并返回
    int

    假设您有一个函数,如:

    int xyz  (const void*, const void*);    
    
    然后您可以将
    xyz
    地址分配给
    fun

    fun = &xyz; 
    
    调用
    qsort()

     compfn  fun; // same as: int (*fun)  (const void*, const void*);
    
    在表达式
    (compfn)compare
    中,您正在将一个函数
    compare
    键入到
    (compfn)
    type函数

    疑问:

     compfn  fun; // same as: int (*fun)  (const void*, const void*);
    
    呼叫不应该是
    (*compfn)

    不,它的类型名不是函数名


    注意:如果你只是写
    int(*compfn)(const void*,const void*)
    comfn
    将是一个指向函数的指针,该函数返回
    int
    ,并接受两个
    const void*
    类型的参数。它是一种函数指针。指向的函数返回
    int
    并接受两个
    const void*
    参数。

    类型定义声明为特定类型创建别名。这意味着它可以用作声明和定义中的任何其他类型

    所以如果你有,例如

    typedef int (*compfn)(const void*, const void*);
    
    然后,您可以仅使用
    compfn
    而不是整个函数指针声明来声明变量或参数。例如,这两种声明是相等的:

    compfn function_pointer_1;
    int (*function_pointer_2)(const void*, const void*);
    
    两者都创建一个函数指针变量,唯一的区别是变量名的名称


    当您有长的和/或复杂的声明时,使用
    typedef
    是很常见的,这样可以简化此类声明的编写并使其更易于阅读。

    谢谢!但是,也请解释一下这个代码“typedefint(*compfn)”。我们到底有什么“typedeffed”,int(*compfn)或int(compfn)。@DrefD
    typedefint(*compfn)
    不正确,
    typedeffed
    也有错误
    int(*compfn)
    应该是
    int*compfn
    @DrefD现在有点困惑了,请参见我在定义
    新类型
    的语法中描述的内容,这是一种函数类型。您询问的部分内容无效。@DrefD您的typedefded
    int(*)(const void*,const void*)
    comfn
    使用我上面描述的语法。@GrijeshChauhan,请参阅对“int(*compfn)应该是int*compfn”的回应。我想后一种说法是不对的,PS:你的最后一个音符消除了我的一些疑虑!