/* 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. 申请IPV6地址配置IPV6域名

    0. 前言 最近弄了一下IPV6,虽然不知道什么时候会用到,但是服务器支持IPV6,还是有必要的. 1. 申请IPV6地址 https://tunnelbroker.net/ 到这个网址去注册一个帐号 ...

  2. Lua 5.1 5.3 参考手册

    Lua 5.1 参考手册: https://www.codingnow.com/2000/download/lua_manual.html Lua 5.3 参考手册: http://cloudwu.g ...

  3. ASP.NET Core Razor Pages

    Razor 页面是Asp.Net Core2.0新增的一个功能.Razor 页面是 ASP.NET Core MVC 的一个新特性,它可以使基于页面的编码方式更简单高效. 环境:vs2017 .net ...

  4. Oracle数据导入指定表空间

    1. 打开工具Oracle SQL Plus 以dba身份登录sys用户 sqlplus /nologconn sys@url as sysdba 2. 创建用户并指定表空间 使用客户端工具或者Web ...

  5. Sass编译时Invalid US-ASCII character解决办法

    编译scss文件时,如果出现如下错误 Error: Invalid US-ASCII character "\xC2" on line 63 of src/assets/_scss ...

  6. python安装模块

    pychram安装模块,非常简单!

  7. CentOS7主机名修改

    1.命令行更改主机名,重启终端即生效. #hostnamectl --static set-hostname k8s-master

  8. talk is cheap, show me the code——dcgan,wgan,wgan-gp的tensorflow实现

    最近学习了生成对抗网络(GAN),基于几个经典GAN网络结构做了些小实验,包括dcgan,wgan,wgan-gp.坦率的说,wgan,wgan-gp论文的原理还是有点小复杂,我也没有完全看明白,因此 ...

  9. [React] 04 - Intro: mongoDB becomes popular

    Ref: Linux平台安装MongoDB - 菜鸟教程 安装:sudo apt-get install mongodb 安装完毕产生: ls /etc/init.d/mongodb 配置:vim / ...

  10. 【GIS】Vue esri-leaflet

    1.npm install esri-leaflet --save 2.引入包 import Leaflet from "leaflet"; var esri = require( ...