如何从一个空指针数组中的项,当我知道每个元素的大小? [英] How to get an item from a void pointer to array when I know each element's size?

查看:135
本文介绍了如何从一个空指针数组中的项,当我知道每个元素的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是当我想要写我自己的教育用途快速排序。这是我得到了什么:

It comes when I want to write my own quicksort for educational purpose. This is what I got:

qsort(void* array, int count, int size, int(*compare)(const void*, const void*));

和我在阵列中的每个元素的大小,指针数组的第一个元素。我怎样才能得到每个元素数组中?

And I have size of each element in array, and pointer to the first element in array. How can I get each individual element in that array?

推荐答案

如果用的sizeof 运营商生成的大小,它是的倍数的sizeof(字符)(是1,定义)。所以投了无效* 的char * ,并移动尺寸人物在同一时间。

If size was generated with the sizeof operator, it is a multiple of sizeof(char) (which is 1 by definition). So cast the void* into a char*, and move size "characters" at a time.

(((char*)array) + i*size)

这篇关于如何从一个空指针数组中的项,当我知道每个元素的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆