排序指针数组 [英] Sorting an array of pointers

查看:111
本文介绍了排序指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是正确的思维是精细治疗的指针作为排序指针数组,例如而言一个int。

Am I right in thinking it is fine to treat a pointer as an int for the purposes of sorting an array of pointers, e.g.

qsort(ptrs, n, sizeof(void*), int_cmp);

我要的师生比排序,以确定是否有任何重复,无论指针指向的东西的类型,所以的qsort是precursor来这样做。

I want to sort the ptrs to ascertain whether there are any duplicates, irrespective of the type of thing the pointer is pointing to, so the qsort is a precursor to doing that.

我的 int_cmp()是pretty标准,例如

my int_cmp() is pretty standard, e.g.

int int_cmp(const void *a, const void *b)
{
    const int *ia = (const int *)a; // casting pointer types
    const int *ib = (const int *)b;

    /* integer comparison: returns negative if b > a
    and positive if a > b */
    return *ia  - *ib;
}

似乎在我的单元测试工作,但有一些原因考虑PTR为int可能会导致这样的,我可能忽略了一个场景中的问题?

it seems to work in my unit-tests but is there some reason why considering a ptr as an int may cause problems for such a scenario that i may have overlooked?

推荐答案

没有,你没有权利在所有的,除非你的希望的通过地址指针进行排序。实际地址很少有任何意义了,所以这是非常不可能的。

No, you're not right at all, unless you want to sort the pointers by address. The actual address rarely has any meaning though, so that's very unlikely.

有关检测重复的指针,你应该只比较指针正因为如此,这是明确的。

For detecting duplicate pointers, you should just compare the pointers as such, that's well-defined.

我想一个解决方案使用可能会去 uintptr_t形式

I would probably go with a solution using uintptr_t:

static int order_pointers(const void *pa, const void *pb)
{
  const uintptr_t a = *(void **) pa, b = *(void **) pb;

  return a < b ? -1 : a > b;
}

没有测试过这一点,但这样的事情应该工作。

Haven't tested this, but something like that should work.

uintptr_t形式转换为是必要的,因为你不能有效地比较随机指针。我答曰C99标准草案,§6.5.8.5:

The conversion to uintptr_t is necessary since you cannot validly compare random pointers. I quoth the C99 draft standard, §6.5.8.5:

当两个指针进行比较,其结果取决于在相对位置
  对象的地址空间指向。如果两个指针对象或不完整的类型都
  指向同一个对象,或两者一点一点过去相同的数组对象的最后一个元素,
  他们比较平等的。如果对象指向是同一个聚合对象的成员,
  指针结构体成员不是成员指针宣告以后更大的比较
  此前在结构中声明,以及指向数组元素具有较大的标
  值进行比较不是指针更大,以相同的数组中的元素以较低的标
  值。所有指向同一个联盟对象的成员比较平等的。如果
  前pression p指向数组对象的元素和前pression Q指向
  相同的数组对象的最后一个元素,指针前pression Q + 1比较大于
  P. 在其他情况下,该行为是不确定的。

When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object or incomplete types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.

我加粗的最后一句,因为那是什么在这里适用。

I bolded the final sentence since that's what applies here.

这篇关于排序指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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