I’m trying to sort an pointer array using pointers instead of indexes but I’m not entirely sure on how to do this. I’ve been googling but haven’t found anything releveant.
I’ve got the sorting to work just fine using indexes but I want to do it by using pointers too. Currently the function looks like this:
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;
}
}
}
}
As you can see the array indexes is being used, how would I do this using only the pointer?
It would be quite annoying. You need to use the fact that in C,
a[i] == *(a + i), and thus this:would become
and so on. There’s really no difference, except that the indexing code is far easier to read. 🙂