简体   繁体   English

使用 function 指针时交换数组中的元素不起作用

[英]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..所以我想使用 AscendingSort() 和 DecendingSort() 作为参数,但似乎在返回值后交换部分被跳过了,希望有人向我解释,谢谢..

        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 == .您可能打算使用=运算符而不是==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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