0

I have to print pointer parameters in ascending order and I was trying to use bubble sort algorithm, but the compiler doesn't sort in the right order. I'm only allowed to use one function.

void sort3(int *a, int *b, int *c){   
        int array[3]= {*a, *b, *c};
        int temp, i,j;
        for(i=0;i<3;i++){
            for(j=0;j<3-1;j++){
                if(array[j]>array[j+1]){
                    temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                }
            }
        }
    }
  • Possible duplicate of [When to use references vs. pointers](https://stackoverflow.com/questions/7058339/when-to-use-references-vs-pointers) – Aras Sep 22 '19 at 14:05
  • 1
    @Aras This question is about C, the one you linked only applies to C++. Why do you think it is a duplicate? – walnut Sep 22 '19 at 16:08
  • algorithm in any language is the same as just syntax is different, you know that. so because of this, that question is duplicate. and if you have a problem on C++ syntax change of your title to find a solution. also if I do something wrong, I'm sorry about that. have a great day and enjoy coding ;) – Aras Sep 23 '19 at 09:03

1 Answers1

0

You sort the local array array, but you don't "return" the values back to the calling function.

Without knowing the full context it seems like you should assign back to what the pointers are pointing to:

*a = array[0];
*b = array[1];
*c = array[2];
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Oh yes, my problem was that I was writing it other way. array[0] = *a and etc. Sorry I'm very new to this – user12102361 Sep 22 '19 at 11:58
  • @user12102361 That's actually a different problem, and should have warranted a different answer. In the future please try to create a [mcve] to show us. Also please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Sep 22 '19 at 12:22