简体   繁体   中英

Swapping elements in array doesn't work when using function pointer

So i want to use AscendingSort() and DecendingSort() as an argument but it seems like after return the value the swap part just get skipped, hope someone explain to me, thanks..

        bool AscendingSort(int a, int b)
        {
            return a > b;
        }
        bool DecendingSort(int a, int b)
        {
            return a < b;
        }
    
        void SortArray(int* a, int size, bool(*func)(int, int))
        {
            int saveElement;
            for (int x = 0; x < size; x++)
            {
                for (int y = x + 1; y < size; y++)
                {
                    if (func(a[x], a[y]))
                    {
                        saveElement = a[x];
                        a[x] == a[y];           //Those 2 lines getting skipped.
                        a[y] == saveElement;
                    }
                }
            }
        }
    
    void main()
    {
        int a[1000];
    
        int arrSize;
    
        SortArray(a, arrSize, AscendingSort);
    
    };

You probably meant to use = operator instead of == .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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