[skill][c] *(char**)
/* 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**)的更多相关文章
- ER 和 数据库关系模式
http://lianghuanyue123.blog.163.com/blog/static/130423244201162011850600/ 我们眼下所接触的数据库基本上是关系数据库,关系数据库 ...
- ER TO SQL语句
ER TO SQL语句的转换,在数据库设计生命周期的位置如下所示. 一.转换的类别 从ER图转化得到关系数据库中的SQL表,一般可分为3类: 1)转化得到的SQL表与原始实体包含相同信息内容.该类转化 ...
- get skill
Get Skill 2018-01-16 > 001 防止数组越界的一种方法 ]; array[n%] = value; > 002 超时机制 在等待某个事件或标志时,设定一定时限,时限到 ...
- [ Skill ] 文件读写 & IO 句柄
https://www.cnblogs.com/yeungchie/ 在 Skill 中使用一种叫做 ioport 类型的变量来操作文件.不过我一般更习惯称为 IO 句柄 (IO/File Handl ...
- 如何区别char与varchar?
1.varchar与char两个数据类型用于存储字符串长度小于255的字符,MySQL5.0之前是varchar支持最大255.比如向一个长度为40个字符的字段中输入一个为10个字符的数据.使用var ...
- 把int*传值给char*,打印出错误的数字
首先进入debug模式查看i的地址也就是ptr的值 以16进制位小端模式存储(一个整型四个字节,8位16进制数)(根据系统位数情况) 紧接着因为ptr是char*型指针变量,读取数据时按照一个字节一个 ...
- 计算机程序的思维逻辑 (8) - char的真正含义
看似简单的char 通过前两节,我们应该对字符和文本的编码和乱码有了一个清晰的认识,但前两节都是与编程语言无关的,我们还是不知道怎么在程序中处理字符和文本. 本节讨论在Java中进行字符处理的基础 - ...
- 数据库中char与varchar类型的区别
在建立数据库表结构的时候,为了给一个String类型的数据定义一个数据库的数据库类型,一般参考的都是char或者varchar,这两种选择有时候让人很纠结,今天想总结一下它们两者的区别,明确一下选择塔 ...
- JAVA 各种数值类型最大值和最小值 Int, short, char, long, float,&nbs
转载地址:http://blog.sina.com.cn/s/blog_5eab3d430101fdv6.html 代码片段: fmax = Float.MAX_VALUE; fmin = Float ...
随机推荐
- 浅谈.net中事务
.net中的事务 关键几点 概念:1:什么是事务 2:什么时候用事务 3:基本的语法 (1): 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务通常 ...
- Ubuntu 卸载重装 IntelliJ Idea Community
参考: https://stackoverflow.com/questions/22983101/how-to-uninstall-intellij-idea-on-ubuntu-13-10 @SLH ...
- 浅析tornado 中demo的 blog模块
#!/usr/bin/env python # # Copyright 2009 Facebook # # Licensed under the Apache License, Version 2.0 ...
- 【WPF】点击滑动条(Slider),移动滑块(Tick)到鼠标点击的位置
问题:点击Slider控件时,滑块会自动跳到滑动条的最边缘位置,无法跳到鼠标点击的位置上. 办法:给Slider控件设置属性IsMoveToPointEnabled="True"即 ...
- Java知多少(15)字符串
从表面上看,字符串就是双引号之间的数据,例如“微学苑”.“http://www.weixueyuan.net”等.在Java中,可以使用下面的方法定义字符串: String stringName ...
- Java知多少(67)面向字符的输入流
字符流是针对字符数据的特点进行过优化的,因而提供一些面向字符的有用特性,字符流的源或目标通常是文本文件. Reader和Writer是java.io包中所有字符流的父类.由于它们都是抽象类,所以应使用 ...
- Java知多少(91)对话框
对话框是为了人机对话过程提供交互模式的工具.应用程序通过对话框,或给用户提供信息,或从用户获得信息.对话框是一个临时窗口,可以在其中放置用于得到用户输入的控件.在Swing中,有两个对话框类,它们是J ...
- python安装模块
pychram安装模块,非常简单!
- Cisco HSRP热备份路由器协议配置
HSRP热备份路由器协议: HSRP是 Hot Standby Routing Protocol(热备份路由协议)的缩写,它的作用是当核心路由器不能正常工作时, 备用路由器能够实现自动平滑的替换,以保 ...
- 使用 GCD 实现倒计时效果
效果如下: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController @p ...