gpt4 book ai didi

C 中的复数定义

转载 作者:太空宇宙 更新时间:2023-11-04 07:00:23 25 4
gpt4 key购买 nike

因为我一直很困惑,有人能解释一下下面例子中的 *(int*)a*(int*)b 是什么,类型转换或一些复杂的指针声明?我需要使用 bsearch 功能。谢谢

int cmpfunc(const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}

最佳答案

这是类型转换。 aconst void * 类型,首先被转换为 int *,然后 int *被取消引用以读取 int

分解:

  • a:输入const void *
  • (int *)a:输入int *
  • *(int *)a:输入int

这样做的原因是因为 int (*)(const void *, const void *) 类型的函数需要传递给 bsearch 并且qsort 作为回调函数。将参数定义为 const void * 允许 qsort 通过让回调函数将参数转换为适当的类型来对任意数据进行排序。

关于C 中的复数定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39429254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com