【问题标题】:Calling a pointer function in C++在 C++ 中调用指针函数
【发布时间】:2015-08-31 20:48:25
【问题描述】:

我用 C++ 编写了一段代码。我从搜索引擎结果中提取了第一部分。

1) 使用double **filter_2d定义函数是什么意思?可以用指针定义函数吗?

2) 我对以下行感到困惑:

double **filt_out = filter_2d(A, 3, 3, B, 2, 1);

它不能正常工作,我不明白为什么。

#include <iostream>
#include <stddef.h>
#include <cmath>
#include <fftw3.h>
using namespace std;

void filter_2d(double** image, int width_image, int height_image, double** kernel, int width_kernel, int height_kernel, double *** OutImg)
{
    double **output = *OutImg;
    int i, j, p, q;

    //this is the case of 'full' option selected in matlab
    //double **output = (double **)malloc(sizeof(double *)*(width_image + width_kernel - 1));
    for (i = 0; i<width_image + width_kernel - 1; i++)
    {
        output[i] = (double *)malloc(sizeof(double)*(height_image + height_kernel - 1));
    }

    //for each point in the output
    for (i = 0; i<width_image + width_kernel - 1; i++)
    {
        for (j = 0; j<height_image + height_kernel - 1; j++)
        {
            output[i][j] = 0;
            //kernel(p,q)*image(i-p, j-q) 
            for (p = 0; p<width_kernel; p++)
            {
                //avoid unnecessary comparisons
                if (i - p < 0)
                {
                    break;
                }
                else if (i - p < width_image)
                {
                    for (q = 0; q<height_kernel; q++)
                    {
                        //idem as above
                        if (j - q < 0)
                        {
                            break;
                        }
                        else if (j - q < width_image)
                        {
                            output[i][j] += kernel[p][q] * image[i - p][j - q];
                        }
                    }
                }
            }
        }
    }
}

int main()
{
    double ** OutImage = 0;
    OutImage = (double **)malloc(sizeof(double *)*(3 * 3));

    double A[3][3] = { { 1, 2, 3 },
    { 4, 5, 6 },
    { 7, 8, 9 } };
    double *A_ptr[9];
    for (int i = 0; i < 10; i++)
    {
        A_ptr[i] = A[i];
    }
    double B[1][2] = { 1, 2 };
    double *B_ptr[2];
    for (int i = 0; i < 2; i++)
    {
        B_ptr[i] = B[i];
    }
    //Error in the below line
    filter_2d(A_ptr, 3, 3, B_ptr, 2, 1, &OutImage);  //unable to understand
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 4; j++)
            cout << *OutImage << endl;
    }
    system("PAUSE");
    return 0;
}

【问题讨论】:

  • ** 是指向指针的指针,或者在您的情况下是指向二维数组的指针。 double** filter_2d 表示函数filter_2d 的返回类型是一个指向二维双精度数组的指针。
  • 请提供比第 66 行 更好的方法来找到您的问题行,我绝对不会数行。 :-/
  • 欢迎使用 C++。汗桶试图将矩阵建模为double**。意识到为什么这是一个坏主意(锯齿状边缘,到处分配的内存)。然后将整个批次装箱并使用 BLAS。 www.boost.org.
  • @Rohit:第 65 行:双 **filt_out = filter_2d(A, 3, 3, B, 2, 1);
  • 详细了解C++ programming,然后使用标准containers,例如std::vector

标签: c++ pointers multidimensional-array


【解决方案1】:

是的,函数可以返回指针,甚至可以返回指向指针的指针。我相信您的两个答案都由this thread 解决。

【讨论】:

  • 那么,我应该如何调用这个函数并打印它的输出呢?我使用双 **filt_out = filter_2d(A, 3, 3, B, 2, 1);并且 msvc10 显示语法错误!
【解决方案2】:

您的函数需要double**,而您正在传递double [3][3]。这些类型没有隐式转换。

您需要在main() 中将数组创建为double **,并将其用作函数调用中的参数。

这个问题 - conversion of 2D array to pointer-to-pointer 应该可以帮助您实现您想要做的事情。

您的cout 似乎也不正确。您正在考虑将 filt_out 作为二维数组而不是指针。

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 4; j++)
    cout  << **(filt_out + i + j) << endl;   //changed here
}

【讨论】:

  • 谢谢,该链接帮助我更正了代码。但是,我仍然看不到输出,您知道吗?
  • @Sam - 我已更新我的答案以包含该部分。
  • 谢谢,但还是报错,无法编译!
  • 你能添加错误吗?或者尝试用谷歌搜索它。 Filt_out 是您的偏移量,您需要向其中添加索引以获取项目的位置。然后执行 ** 将返回该位置的内容。
  • 这里是错误:访问冲突读取位置0xfdfdfdfd,我也觉得结果很奇怪!因为它应该是这样的:2 5 8 3 8 14 17 6 14 23 26 9
【解决方案3】:

在 C++ 指针上下文中将 * 读取为 pointer to 会有所帮助。

int* a;

a 是一个指向 int 的指针。

int** b;

b 是指向int 的指针。

b = &a;

a 是指向int 的指针。 &amp;a 是指向int 的指针的地址。 b 是一个指向指向 int 的指针。

*a = 10;

将10存储在a指向的内存中。

**b = 20;

将20存储在b指向的int*指向的内存中。

#include <iostream>

int main()
{
    int i = 1234;
    int* a;
    int** b;

    std::cout << "i is " << i << ", it's address is " << i << "\n";

    a = &i;

    std::cout << "a = " << a << ", *a = " << *a << ", its address is " << &a << "\n";

    b = &a;
    std::cout << "b = " << b << ", *b = " << *b << ", **b = " << **b << ", its address is " << &b << "\n";
}

现场演示:http://ideone.com/OpCro4

您的函数“filter_2d”返回一个指针的地址。它还期望第一个参数是指针的地址。

这通常被用作允许函数说“给我一个指针的地址,我会为你填充它”的一种方式,但 C++ 也使用指针来传递数组。

int a[100];
f(a);

程序可以将所有 100 个地址传递给 f(),这需要堆栈上的 100 个整数或 100 个寄存器。

或者,它可以传递 a 中第一个 int 的地址。在 C 和 C++ 中,这通常是数组的工作方式 - 它们作为数组和偏移量进行操作。

int a[100];
int* b = a;  // b points to the first element in a

// these two mean the same thing
a[90];
*(b + 90);

// undefined behavior
*(b + 100); // the 101st element of a, i.e. invalid

缺点:指针只知道它们指向的元素,它们本质上对数组长度一无所知。

最后,不要使用SYSTEM("PAUSE"),而是使用“Ctrl+F5”启动而不进行调试(执行后会自动提示您按回车键)或使用“F11”进入您的程序。

【讨论】:

  • 我应该打印 filt_out[i][j] 还是 **filt_out[i][j] 来查看结果?!
【解决方案4】:

指针声明
一般格式:

data_type *pointer_name;

一个指针声明,例如,

int *numberPtr; 

将 numberPtr 声明为指向整数变量的变量。它的内容是一个内存地址

*表示被声明的变量是指针变量,而不是普通变量。

考虑以下声明:

int *numberPtr, number = 20;  

在这种情况下,两个内存地址已被保留,与名称 numberPtr 和 number 相关联。

变量number中的值是整数类型,变量numberPtr中的值是另一个内存位置的地址。

例子

// create a 2D array dynamically
int rows, columns, i, j;
int **matrix;
cin >> rows >> columns;
matrix = new int*[rows];
for(i=0; i<rows; i++)
   matrix[i] = new int[columns];

【讨论】:

    【解决方案5】:

    您的代码有 2 个问题:

    首先,我假设输出图像的大小与输入图像的大小相同,因此它必须像这样分配:

    (double **)malloc(sizeof(double *)*(width_image * height_image));
    

    其次,你定义了一个返回 2D 指针的函数,但不幸的是,你在函数内部声明了这个 2D 指针,这意味着你定义了一个局部变量指针,在大多数情况下,一旦你返回这个值,它将完全错了,不是在函数内部分配的那个。

    要解决问题,您可以选择以下两种解决方案之一:

    1. 您可以定义一个全局 2D 指针,并在您的函数内部分配它,因此您无需定义您的函数来返回 2D 指针。
    2. 第二种解决方案是定义将结果存储在调用函数中的二维指针,调用函数将为该指针分配所需的大小并将其传递给被调用函数(即filter_2d),当它传递它时,它将通过其地址传递,因此在 filter_2d 定义中,我们将添加一个额外的参数作为 3D POINTER 来存储结果,如下所示:

    //Define these 2 lines in the main function.
        
        double ** OutImage = null;
        OutImage = (double **)malloc(sizeof(double *)*(width_image * height_image));

    将 OutImage 传递给 filter_2d 函数:

    filter_2d(A_ptr, 3, 3, B_ptr, 2, 1, &OutImage);
    

    filter_2d函数的定义应该是:

    void filter_2d(double** image, int width_image, int height_image, double** kernel, int width_kernel, int height_kernel, double *** OutImg)
    

    在 filter_2d 中,您可以如下定义局部变量:

    double **output = *OutImg;
    

    希望这种校准对您有所帮助。

    【讨论】:

    • 谢谢,您的解释非常有用 我按照您的建议更新了代码,但是,我想我打印错误的输出,是吗?我尝试同时打印 *OutImage 和 OutImage ,它们只是一些地址
    • 您能解释一下您想如何打印输出吗?要打印输出图像中的全部值,只需使用从 0 到 8 开始的一个循环。但是每次都必须更改指针位置,因此在打印值后需要添加以下行:OutImage++;
    • 我想将输出打印为单列。正如你所说,我使用了这个循环,再次看不到输出,只看到一些地址: for (int i = 0; i
    • 在你的 filter_2d 函数中,你为每一行分配这个大小 (height_image + height_kernel - 1),这将在你的数组中产生一个 9 * (height_image + height_kernel - 1) 字节...帮助你,请给我解释一下你到底想要做什么。
    • 如果代码工作正常,我应该得到这个输出:2 5 8 3 8 14 17 6 14 23 26 9
    【解决方案6】:

    我分析了你的代码,我想我发现了一些问题。

    这是新代码:

    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    
    double** filter_2d(double** image, int width_image, int height_image, double** kernel, int width_kernel, int height_kernel)
    {
        int i, j, p, q;
    
        //this is the case of 'full' option selected in matlab
        double **output = (double **)malloc(sizeof(double *) * (width_image + width_kernel - 1));
        for (i = 0; i<width_image + width_kernel - 1; i++)
            output[i] = (double *)malloc(sizeof(double) * (height_image + height_kernel - 1));
    
        //for each point in the output
        for (i = 0; i<width_image + width_kernel - 1; i++)
            for (j = 0; j<height_image + height_kernel - 1; j++)
            {
                output[i][j] = 0;
                //kernel(p,q)*image(i-p, j-q) 
                for (p = 0; p<width_kernel; p++)
                {
                    //avoid unnecessary comparisons
                    if (i - p < 0)
                    {
                        break;
                    }
                    else if (i - p < width_image)
                    {
                        for (q = 0; q<height_kernel; q++)
                        {
                            //idem as above
                            if (j - q < 0)
                                break;
                            else if (j - q < width_image)
                                output[i][j] += kernel[p][q] * image[i - p][j - q];
                        }
                    }
                }
            }
    
        return output;
    }
    
    
    int main()
    {
        double A[3][3] = { { 1, 2, 3 },
                           { 4, 5, 6 },
                           { 7, 8, 9 } };
        double *A_ptr[9];
    
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j ++)
                A_ptr[i * 3 + j] = &(A[i][j]);
    
    
        double B[1][2] = { 1, 2 };
        double *B_ptr[2];
    
        for (int i = 0; i < 1; i++)
            for (int j = 0; j < 2; j ++)
                B_ptr[i * 1 + j] = &(B[i][j]);
    
        //no more errors in the function call
        double **OutImage = filter_2d(A_ptr, 3, 3, B_ptr, 2, 1);
    
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 3; j++)
                cout << OutImage[i][j] << " ";
            cout << endl;
        }
    
    
        return 0;
    }
    

    我认为一个更好的主意是函数 filter_2d 返回一个指向输出矩阵的指针。输出矩阵是通过函数内部的 malloc 动态分配的,所以如果将地址返回给它并存储回 main 中,它就不会丢失(并且可以获取矩阵中的计算值)。

    您可以在此处看到堆栈内存和函数本地变量与堆内存和使用 malloc stack vs heap 分配的变量之间的比较

    下面说说我在main函数中发现的一些问题。第一个问题是指针数组 A_ptr 和 B_ptr 的初始化。

    double *A_ptr[9];
    for (int i = 0; i < 10; i++)
    {
        A_ptr[i] = A[i];
    }
    

    double *B_ptr[2];
    for (int i = 0; i < 2; i++)
    {
        B_ptr[i] = B[i];
    }
    

    根据我在您的代码中的理解,A_ptr 和 B_ptr 的元素是指向数组 A 和 B 的每个元素的指针。

    因此,由于 A_ptr 和 B_ptr 是线性化矩阵,因此您必须小心提供数组 A 和 B 中相应元素的正确地址。

    如果你把一个矩阵 M 线性化成一个矩阵 N,那么元素 M[i][j] 将转到 N[i * number_of_columns_from_M + j]。

    另一个问题是您打印结果的 for 循环中 i 和 j 的限制。

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 4; j++)
            cout << *OutImage << endl;
    }
    

    根据我的计算,在 filter_2d 函数中,您分配了一个 4 行 3 列的矩阵。在那些周期中,您假设 OutImage 有 5 行和 4 列。

    最后一个问题是从 OutImage 打印元素。

    cout << *OutImage << endl;
    

    您在代码中声明的 OutImage 是一个由 9 个指针组成的数组(不明白您为什么这样做)。使用上述指令,您将重复打印 OutImage 数组的第一个元素(这是一个地址,因为 OutImage 是一个由 9 个指针组成的数组),这就是为什么您只看到打印的地址。

    我不确定现在屏幕上打印的数字是否正确,因为我不知道 filter_2d 中做了什么数学计算。

    【讨论】:

    • 谢谢,我收到此错误:C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xmemory(208): error C2440: 'initializing' : cannot convert from 'double *'到'双'。没有可以进行这种转换的上下文
    • 出现该错误的指令是什么?该程序在我的计算机上运行没有问题。
    • 我在之前的评论中发布了错误,没有帮助吗?
    • @Sam 在上面的代码中包含 stdlib.h 以避免错误。
    【解决方案7】:

    我用 C++ 编写了一段代码。我把第一部分从 搜索引擎结果。

    你是认真的吗?不知道怎么理解。它不是一个调试站点。你应该先努力。

    无论如何,您的代码大部分是 C。唯一让我想起 C++ 的代码是控制台输出。所以,如果我能帮上忙,让我试试……因为我喜欢。

    1) 使用double**filter_2d定义函数是什么意思?可以用指针定义函数吗?

    这意味着函数的结果是一个指向双精度类型指针的指针。像这样分解它:

    • **filt_out 的类型为 double - 用于访问双精度值; 在二维数组中常用来访问二维,即二维数组的行和列

    • *filt_out 的类型为 double * - 用于访问指向双精度值的指针; 在 2D 数组中常用来访问第一个维度,即 2D 数组的行

    • filt_out 的类型为double ** - 用于访问指向双精度值指针的指针; 在二维数组中常用来访问数组,即为二维数组分配的内存地址

    您可以使用 simple 指针定义函数,但它不适用于 2D 数组。阅读以上内容。

    2) 我对以下行感到困惑:

    double **filt_out = filter_2d(A, 3, 3, B, 2, 1); 不工作 正确,我不明白为什么。

    对我来说没有意义。 filter_2d 的返回类型是 void,因此我不明白为什么要将返回的 value 分配给 指向双精度指针的指针

    它不能正常工作,我不明白为什么。

    我也没有。但老实说,这听起来更像是一个调试请求,而不是一个值得投票的问题。特别是你给我们的印象是你首先没有做功课学习C/C++,其次是从搜索引擎复制代码并要求社区为你解决。


    一些缺陷我相信你想仔细看看:

    (我将主要使用 C 语法)

    OutImage = (double **)malloc(sizeof(double *)*(3 * 3));
    

    我觉得不合适。请验证。

    我认为 OutImage 应该是一个二维数组(图像),因此 **OutImage 指向二维数组的一个元素(第二维,您要访问行和列)。

    另外,由于它是一个二维数组,您需要先初始化第一个维度(即行),然后是第二个维度(即列)。

    所以我会建议这样的事情:

    //three rows of size for type double*
    OutImage = (double **) malloc(sizeof(double *) * 3);
    
    //three columns of size of type double
    for (int i=0; i<3; i++)
        OutImage[i] = (double *) malloc(sizeof(double) * 4);
    

    这样您就可以使用OutImage[row][column] 访问。我相信它不太容易出错。我根据计算宽度和高度的函数filter_2d 中的计算将列的大小设置为4(宽度与给定的参数保持相同,高度增加一维)。 另外(见下文)函数 filter_2d 的后面部分我将删除内存分配,因为它已经在这里完成了。


    不确定你想用这个实现什么,但我认为......

    double *A_ptr[9];
    for (int i = 0; i < 10; i++)
        {
            A_ptr[i] = A[i];
        }
    

    在很多层面上都是错误的。

    • 10 没有意义;指数从 0 到 8
    • A[i] 的大小为 3,而 A_ptr[i] 的大小为 9
    • 山姆你在想什么?

    考虑到在上面的函数filter_2d 中使用 A_ptr(以及访问它的方式),我认为您想做一些类似于上述二维数组的事情。

    double ** A_ptr = (double **) malloc(sizeof (double *) * 3);
    for (int i = 0; i < 3; i++)
        A_ptr[i] = (double *) malloc(sizeof (double) * 3);
    
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            A_ptr[i][j] = A[i][j];
        }
    }
    

    double B[1][2] = { 1, 2 };
    double *B_ptr[2];
    for (int i = 0; i < 2; i++)
    {
        B_ptr[i] = B[i];
    }
    

    与上面类似。

    • B[i] 的大小为 1,因此只有索引 0 才有意义
    • 该死的山姆,你又在想什么?

    您使用以下参数调用过滤器:

    • A_ptr:A(图像)的二维数组副本
    • 3:图像第一维的大小
    • 3:图像第二维的大小
    • B_ptr:B(内核)的二维数组副本
    • 2:内核第一维的大小 - 应该与下一个切换
    • 1:内核第二维的大小-应该和前一个切换
    • &OutImage:指向过滤后图像的指针地址(参数实际上是指向**OutImage的指针)?我认为您想在函数调用后保留指针,不是吗?对我来说听起来不错。

      filter_2d(A_ptr, 3, 3, B_ptr, 2, 1, &OutImage);

    您将 B_ptr 定义为具有维度 [1][2] 的 B 的副本,但您将 2 作为第一个维度和 1 作为第二个维度传递给函数。要么切换B/B_ptr的维度,要么切换两个参数。

    在该函数中,我将删除以下代码

    for (i = 0; i<width_image + width_kernel - 1; i++)
    {
        output[i] = (double *)malloc(sizeof(double)*(height_image + height_kernel - 1));
    }
    

    (在为OutImage 分配内存时,请参阅上面第一个错误中的最后一句话)。


    替换循环以打印结果。让它看起来像这样:

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++)
            cout << OutImage[i][j] << endl;
    }
    

    我保留了 C++ 样式的打印,但实际上您也可以简单地使用 C 的 printf 函数来完成。真的不需要包含iostream


    就是这样。我编译了你的代码并运行它。不知道会发生什么,但根据您的评论应该是

    2 5 8 3 8 14 17 6 14 23 26 9
    

    你猜怎么着?我得到了

    1 4 7 6 4 13 16 12 7 22 25 18
    

    好吧,我想现在该轮到你了。

    请记住,检查您要在哪里进行内存分配 为了让它考虑到新的维度。我很难 在您的示例中对其进行编码以使其或多或少地工作。

    我可能会分配一个虚拟地址,然后使用realloc 根据参数将大小增加到所需的大小。

    请记住,通常您希望释放分配的内存。 因为是小程序,这里略过。

    程序可能如下所示:

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    
    using namespace std;
    
    void filter_2d(double** image, int width_image, int height_image, double** kernel, int width_kernel, int height_kernel, double *** OutImg) {
    
        double **output = *OutImg;
        int i, j, p, q;
    
        int rows = width_image + width_kernel - 1;
        int cols = height_image + height_kernel - 1;
    
        //rows of size for type double*
        output = (double **) realloc(output, sizeof (double *) * rows);
        //columns of size of type double
        for (int i = 0; i < rows; i++)
            output[i] = (double *) malloc(sizeof (double) * cols);
    
        //for each point in the output
        for (i = 0; i < width_image + width_kernel - 1; i++) {
            for (j = 0; j < height_image + height_kernel - 1; j++) {
                output[i][j] = 0;
                //kernel(p,q)*image(i-p, j-q) 
                for (p = 0; p < width_kernel; p++) {
                    //avoid unnecessary comparisons
                    if (i - p < 0) {
                        break;
                    } else if (i - p < width_image) {
                        for (q = 0; q < height_kernel; q++) {
                            //idem as above
                            if (j - q < 0) {
                                break;
                            } else if (j - q < width_image) {
                                output[i][j] += kernel[p][q] * image[i - p][j - q];
                            }
                        }
                    }
                }
            }
        }
    }
    
    int main() {
    
        //allocate dummy memory of size for type double*
        double ** OutImage = (double **) malloc(sizeof (double *));
    
        // define image matrix
        double A[3][3] = {
            { 1, 2, 3},
            { 4, 5, 6},
            { 7, 8, 9}
        };
        // copy image matrix
        double ** A_ptr = (double **) malloc(sizeof (double *) * 3);
        for (int i = 0; i < 3; i++)
            A_ptr[i] = (double *) malloc(sizeof (double) * 3);
    
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                A_ptr[i][j] = A[i][j];
                printf(" %f ", A_ptr[i][j]);
            }
        }
    
        printf("\n");
    
        //define kernel matrix
        double B[1][2] = {
            { 1, 2}
        };
        //copy kernel matrix
        double ** B_ptr = (double **) malloc(sizeof (double *));
        B_ptr[0] = (double *) malloc(sizeof (double)*2);
        for (int i = 0; i < 1; i++) {
            for (int j = 0; j < 2; j++) {
                B_ptr[i][j] = B[i][j];
                printf(" %f ", B_ptr[i][j]);
            }
        }
    
        printf("\n");
    
        //call filter
        filter_2d(A_ptr, 3, 3, B_ptr, 1, 2, &OutImage);
    
        //print result
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 4; j++)
                cout << OutImage[i][j] << endl;
        }
    
        // No idea what that is
        //system("PAUSE");
        return 0;
    }
    

    P.S.:我刚刚看到 Valy 有一个很好的解决方案。

    【讨论】:

    • 谢谢伊利亚辛。我收到此错误:mytry.exe 中 0x000000013fe0167f 的第一次机会异常:0xC0000005:访问冲突读取位置 0xffffffffffffffff。 mytry.exe 中 0x000000013fe0167f 处的未处理异常:0xC0000005:访问冲突读取位置 0xffffffffffffffff。程序“[14704] mytry.exe: Native”已退出,代码为 -1073741819 (0xc0000005)
    • 发生在 //print result
    • 你能提供更多细节吗?哪些操作系统、编译器、编译器选项?这是您编译的唯一代码吗?还是您将其嵌入到另一个代码中?这段代码在 Ubuntu 上适用于我,Valy 的代码也适用于我。老实说,我会检查你的环境。
    • WIN 7、64 位、msvc10。我的项目中没有其他打开的程序或代码。顺便说一句,“该死的山姆,你又在想什么?”是什么意思? ??!!
    • 那更像是个玩笑。这意味着错误是如此明显,以至于您不应该(再次)提交它。你可以忽略它。重要的是您了解错误和更正。
    【解决方案8】:
    #include <stdlib.h>
    
     int int_sorter( const void *first_arg, const void *second_arg )
    {
    int first = *(int*)first_arg;
    int second = *(int*)second_arg;
    if ( first < second )
    {
        return -1;
    }
    else if ( first == second )
    {
        return 0;
    }
    else
    {
        return 1;
    }
    }
    
    int main()
    {
    int array[10];
    int i;
    /* fill array */
    for ( i = 0; i < 10; ++i )
    {
        array[ i ] = 10 - i;
    }
    qsort( array, 10 , sizeof( int ), int_sorter );
    for ( i = 0; i < 10; ++i )
    {
        printf ( "%d\n" ,array[ i ] );
    }
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-16
      • 2020-08-21
      • 1970-01-01
      • 2011-01-29
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多