2

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?

2
  • 1
    Why did you want to do this using pointers? Indices are easier to read. Commented Oct 22, 2012 at 13:37
  • Do you want to sort an array of pointers, based on the ordering of the things the pointers point to? If not, what do you mean by sorting using pointers instead of indices? Commented Oct 22, 2012 at 13:40

3 Answers 3

7

It would be quite annoying. You need to use the fact that in C, a[i] == *(a + i), and thus this:

if(pointer[j] < pointer[j])

would become

if(*(pointer + j) < *(pointer + j))

and so on. There's really no difference, except that the indexing code is far easier to read. :)

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, so it was that simple. Thank you!
4

You can replace your indexes by using pointers:

void sort(int *pointer, int size){
    int *i, *j, temp;
    for(i = pointer; i < pointer + size; i++){
        for(j = i + 1; j < pointer + size; j++){
            if(*j < *i){
                temp = *j;
                *j = *i;
                *i = temp;
            }
        }
    }
}

In theory this should be faster than adding an index each time you need to access an element. In practice most compilers will optimise your original loop to use pointers in this way anyway.

2 Comments

This is a much better solution than using *(a + i) which is really just using indexes in a different way. This one actually uses pointers. Good job. only change I would make is to move the definition of j inside the for (i... loop to reduce its scope and to move temp definition to within the if(*j brackets to reduce its scope.
@RichardChambers: I agree with your suggested changes. I wanted to keep the structure of the original code as much as possible so that the OP could do an easy comparison.
2

The square brackets ([]) are really just an "add and deference" operator. So to convert your code replace the something[x] with a *(something+x)

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;
         }
     }
  } 
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.