/* scmp: string compare of *p1 and *p2 */
int scmp(const void *p1, const void *p2)
{
char *v1, *v2;
v1 = *(char **) p1;
v2 = *(char **) p2; return strcmp(v1, v2);
}

qsort passes to the comparing function a pointer to the elements it has to compare; since in C there are no templates, this pointer is just brutally cast to a const void * (void * in C just means "this is some kind of pointer", and to do something on it you must cast it back to its actual type).

Now, if you are sorting an array of strings, each element you have to compare is a char *; but qsort passes to the comparison function a pointer to each element, so what your scmp receives is actually a char ** (a pointer to a pointer to the first character of the string), casted to a const void * because the signature of the comparison function says so.

So, to get your char *, you have first to convert the parameters to their actual type (char **), and then dereference this pointer to get the actual char * you want to compare.

(although, it would be more correct from a const-correctness point of view to cast to const char **)

http://stackoverflow.com/questions/10364153/pointer-cast-for-use-with-qsort

http://stackoverflow.com/questions/13487821/the-use-of-char/13487867

[skill][c] *(char**)的更多相关文章

  1. ER 和 数据库关系模式

    http://lianghuanyue123.blog.163.com/blog/static/130423244201162011850600/ 我们眼下所接触的数据库基本上是关系数据库,关系数据库 ...

  2. ER TO SQL语句

    ER TO SQL语句的转换,在数据库设计生命周期的位置如下所示. 一.转换的类别 从ER图转化得到关系数据库中的SQL表,一般可分为3类: 1)转化得到的SQL表与原始实体包含相同信息内容.该类转化 ...

  3. get skill

    Get Skill 2018-01-16 > 001 防止数组越界的一种方法 ]; array[n%] = value; > 002 超时机制 在等待某个事件或标志时,设定一定时限,时限到 ...

  4. [ Skill ] 文件读写 & IO 句柄

    https://www.cnblogs.com/yeungchie/ 在 Skill 中使用一种叫做 ioport 类型的变量来操作文件.不过我一般更习惯称为 IO 句柄 (IO/File Handl ...

  5. 如何区别char与varchar?

    1.varchar与char两个数据类型用于存储字符串长度小于255的字符,MySQL5.0之前是varchar支持最大255.比如向一个长度为40个字符的字段中输入一个为10个字符的数据.使用var ...

  6. 把int*传值给char*,打印出错误的数字

    首先进入debug模式查看i的地址也就是ptr的值 以16进制位小端模式存储(一个整型四个字节,8位16进制数)(根据系统位数情况) 紧接着因为ptr是char*型指针变量,读取数据时按照一个字节一个 ...

  7. 计算机程序的思维逻辑 (8) - char的真正含义

    看似简单的char 通过前两节,我们应该对字符和文本的编码和乱码有了一个清晰的认识,但前两节都是与编程语言无关的,我们还是不知道怎么在程序中处理字符和文本. 本节讨论在Java中进行字符处理的基础 - ...

  8. 数据库中char与varchar类型的区别

    在建立数据库表结构的时候,为了给一个String类型的数据定义一个数据库的数据库类型,一般参考的都是char或者varchar,这两种选择有时候让人很纠结,今天想总结一下它们两者的区别,明确一下选择塔 ...

  9. JAVA 各种数值类型最大值和最小值 Int, short, char, long, float,&nbs

    转载地址:http://blog.sina.com.cn/s/blog_5eab3d430101fdv6.html 代码片段: fmax = Float.MAX_VALUE; fmin = Float ...

随机推荐

  1. 自定义progressdialog,改善用户体验

    自定义progressdialog,改善用户体验

  2. 在IDEA中将项目部署到Tomcat的方法及两种模式的区别

    转自:https://www.jianshu.com/p/fb0ed26c35d5 1.添加tomcat服务器 点右上角编辑配置   编辑配置 点击左上角+选择tomcat服务器   添加tomcat ...

  3. Java Socket 死循环while如何判断客户端断开

    多线程的服务器程序 线程中等待客户端的消息    我的代码能实现服务器与客户端的通信    问题是: 当客户端中断或退出  以上代码却不能判断Socket中断 跳不出while的无限循环 解决方法: ...

  4. duilib进阶教程 -- TreeView控件(6)

    代码下载:http://download.csdn.net/detail/qq316293804/6483905 上一个教程中,界面已经和迅雷一模一样啦,大小和位置一个像素都不差哟,亏得Alberl调 ...

  5. 5. RAMN备份与恢复

    一. rman简介 RMAN(Recovery Manager)是一种用于备份(backup).还原(restore)和恢复(recover)数据库的 Oracle 工具.RMAN只能用于ORACLE ...

  6. 《Python3网络爬虫开发实战》

    推荐:★ ★ ★ ★ ★ 第1章 开发环境配置 第2章 网页基础知识 第3章 网络爬虫基础 第4章 基本库的使用 第5章 解析库的使用 第6章 数据存储 第7章 Ajax数据爬取 第8章 动态渲染页面 ...

  7. 关于 oh-my-zsh 插件的使用(以 Sublime Text 为例)

    这里不讲 oh-my-zsh 是什么.也不讲 oh-my-zsh 插件的工作原理(太深奥,不懂 ). 讲一讲作为一个初学者,在使用过程中遇到的问题以及解决方法. 1 缘起 Ubuntu下,编辑/预览 ...

  8. 微信小游戏 main.js没有被压缩

    发布时,发现main.js没有被压缩. 在config.wxgame.ts里增加如下图.

  9. electron 主进程,和渲染进程的通信

    ipcMain https://electronjs.org/docs/api/ipc-main 当在主进程中使用时,它处理从渲染器进程(网页)发送出来的异步和同步信息, 当然也有可能从主进程向渲染进 ...

  10. 录制用户的音频,视屏 navigator.mediaDevices.getUserMedia

    google 文档 HACKS 文档 相关代码 获取本地的音频 <input type="file" accept="audio/*" capture=& ...