简体   繁体   English

qsort如何修改指向字符串的指针?

[英]How qsort modify pointer to string?

The implementation of qsort in glibc modify the array of pointer by swap function as defined like this: glibc中qsort的实现通过交换函数修改指针数组,如下所示:

#define SWAP(a, b, size)                              \
  do                                          \
    {                                         \
      register size_t __size = (size);                        \
      register char *__a = (a), *__b = (b);                   \
      do                                      \
    {                                     \
      char __tmp = *__a;                              \
      *__a++ = *__b;                              \
      *__b++ = __tmp;                             \
    } while (--__size > 0);                           \
    } while (0)

...See the full code here ... ...在这里查看完整代码...

Say, I've declared the array of pointer like this: (I know pointer strings cannot be modified). 说,我已经这样声明了指针数组:(我知道不能修改指针字符串)。

  char *a[] = {"one", "two", "three", "four"},
         *lo = (char*)a,
         *hi = &lo[2];
    SWAP(lo, hi, 4);   // Doesn't work.

In short, I want to know how qsort sort array of pointer to string. 简而言之,我想知道qsort如何对字符串指针数组进行排序。 As far as I know, array of pointer cannot be modified. 据我所知,指针数组无法修改。 It can only point to other pointer. 它只能指向其他指针。

Your initializations of lo and hi are incorrect. 您对lohi初始化不正确。 They should be: 他们应该是:

    char *lo = (char*)&a[0],
         *hi = (char*)&a[2];

This will swap the values of lo and hi . 这将交换lohi的值。 Full code: 完整代码:

#include <stdio.h>

#define SWAP(a, b, size)                                \
    do                                                  \
        {                                               \
            register size_t __size = (size);            \
            register char *__a = (a), *__b = (b);       \
            do                                          \
                {                                       \
                    char __tmp = *__a;                  \
                    *__a++ = *__b;                      \
                    *__b++ = __tmp;                     \
                } while (--__size > 0);                 \
        } while (0)

int main(int argc, char *argv[]) {
    char *a[] = {"one", "two", "three", "four"};
    char *lo = (char*)&a[0], *hi = (char*)&a[2];
    SWAP(lo, hi, sizeof(*lo));
    int i;
    for (i = 0; i < 4; i++) {
        printf("a[%d] = %s\n", i, a[i]);
    }
}

Output: 输出:

a[0] = three
a[1] = two
a[2] = one
a[3] = four

Suppose the string memory is initially like this, starting from address 1000: 假设字符串存储器最初是这样的,从地址1000开始:

one\0two\0three\0four\0

The values of the array are: 数组的值为:

a[0] = 1000 -> one
a[1] = 1004 -> two
a[2] = 1008 -> three
a[3] = 1014 -> four

After the SWAP , the string memory is unchanged, but the array is now: SWAP ,字符串存储器保持不变,但数组现在为:

a[0] = 1008 -> three
a[1] = 1004 -> two
a[2] = 1000 -> one
a[3] = 1014 -> four

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM