使用指针而不是索引进行排序

Kra*_*ffs 2 c arrays sorting

我正在尝试使用指针而不是索引对指针数组进行排序,但我不完全确定如何执行此操作.我一直在谷歌上搜索,但没有找到任何相关的东西.

我已经使用索引完成了排序工作,但我也希望通过使用指针来实现.目前该功能如下所示:

void sort(int *pointer, int size){
    int i, j, temp;
    for(i = 0; i < size; i++){
        for(j = i + 1; j < size; j++){
            if(pointer[j] < pointer[i]){
                temp = pointer[j];
                pointer[j] = pointer[i];
                pointer[i] = temp;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,正在使用数组索引,如何仅使用指针执行此操作?

unw*_*ind 7

这会很烦人.您需要使用C中的事实,a[i] == *(a + i)因此:

if(pointer[j] < pointer[j])
Run Code Online (Sandbox Code Playgroud)

会成为

if(*(pointer + j) < *(pointer + j))
Run Code Online (Sandbox Code Playgroud)

等等.除了索引代码更容易阅读之外,没有什么区别.:)