简体   繁体   English

如何在双指针中返回int数组?

[英]How to return array of int in double pointer?

I need to write a function that returns an array of arrays: 我需要编写一个返回数组数组的函数:

return_array = { {1}, {1,2,3}, {...}, ....};

Each array has a different size. 每个阵列都有不同的大小。 The function must comply to following signature: 该功能必须符合以下签名:

int** generate(int n, int** column_sizes)

n is the input to function and I use that to create the return-array . n是函数的输入,我用它来创建return-array I know how to create return_array , but I don't understand how size of each array should be returned in double pointer int** column_sizes ? 我知道如何创建return_array ,但是我不明白如何在双指针int** column_sizes返回每个数组的大小?

I would just returned them in a single pointer int* column_sizes like below: 我将以单个指针int* column_sizes返回它们,如下所示:

int** generate(int n, int* column_sizes){
    int return_size=some_function(n); 
    int** returned_array=malloc(return_size*sizeof(int*));
    ...
    column_sizes[0]=c0; // First array size
    column_sizes[1]=c1; // Second array size
    ...
    return returned_array;
}

The purpose of the column_sizes parameter is to pass the number of elements in each of the sub-arrays of the returned double-pointer to the caller. column_sizes参数的目的是将返回的双指针的每个子数组中的元素数传递给调用方。

If it is to be allocated inside your function, it has to be a double pointer. 如果要在函数内部分配它,则它必须是双指针。

#include <stdlib.h>
#include <stdio.h>

int** generate(int n, int** column_sizes){


    *column_sizes = malloc(n*sizeof(int));

    for (int i=0; i<n; i++)
        (*column_sizes)[i]=i;

    int** return_array=malloc(n*sizeof(*int));
    for(int i=0; i<n; i++) {
        return_array[i]=malloc((*column_sizes)[i]*sizeof(int));
        for(int j=0; j<(*column_sizes)[i]; j++) {
            // set the j'th value in the i'th array
            return_array[i][j]=i*j;
        }
    }
    return return_array;
}

int main() {
    int *column_sizes;

    int n=4;

    int** arrays= generate(n, &column_sizes);
    printf("%i\n", *column_sizes);


    for(int i=0; i<n; i++) {
        for(int j=0; j<column_sizes[i]; j++) {
            printf("%i %i: %i\n",i,j, arrays[i][j]);

        }
    }
}

There are some unresolved issues in the question, notably: 问题中有一些未解决的问题,特别是:

  • How are the column sizes determined? 如何确定列大小?
  • How will the caller free the allocated memory? 调用方将如何释放分配的内存?

Nonetheless, we can start answering. 尽管如此,我们可以开始回答。 It looks like you need to allocate at least three spaces: One for the column sizes, one for the pointers to the columns, and one for all the actual int data. 看来您需要分配至少三个空间:一个用于列大小,一个用于列的指针,另一个用于所有实际的int数据。 This supposes we put all the int data for all the columns in a single array but point into appropriate places in the array through the column pointers. 假设我们将所有列的所有int数据放在单个数组中,但通过列指针指向数组中的适当位置。 An alternative is to allocate space for each column's data separately. 一种替代方法是分别为每个列的数据分配空间。

In the former case, the function could be: 在前一种情况下,该函数可以是:

int **generate(int n, int **column_sizes)
{
    // Allocate space for columns sizes and assign column sizes.
    int NumberOfColumns = /* Some calculation not explained in question. */;
        // (size_t would be better than int, but I will use the types in the question.)
    int *sizes = malloc(NumberOfColumns * sizeof *sizes);
    // Insert code to abort if malloc failed.
    *column_sizes = sizes;
    int TotalElements = 0;
    for (int i = 0; i < NumberOfColumns; ++i)
    {
        sizes[i] = /* Some calculation to find size of column i. */;
        TotalElements += sizes[i];
    }

    // Allocate space for pointers to columns.
    int **returned_array = malloc(NumberOfColumns * sizeof *returned_array);
    // Insert code to abort if malloc failed.

    // Allocate space for the actual int data.
    int *Space = malloc(TotalElements * sizeof *Space);
    // Insert code to abort if malloc failed.

    // Assign pointers to columns.
    returned_array[0] = Space;
    for (int i = 1; i < NumberOfColumns; ++i)
        returned_array[i] = returned_array[i-1] + sizes[i-1];

    // Fill in the actual int data.
    for (int i = 0; i < NumberOfColumns; ++i)
        for (int j = 0; j < column_sizes[i]; ++j)
            returned_array[i][j] = /* Some unexplained calculation. */;

    return returned_array;
}

With this definition, the caller could free the memory by freeing the array of column sizes, freeing the space pointed to by the first pointer in the returned array, and freeing the returned array. 使用此定义,调用者可以通过释放列大小的数组,释放返回的数组中第一个指针指向的空间以及释放返回的数组来释放内存。 If, in an alternative implementation, each column is allocated separately, the caller would have to free each pointer in the returned array. 如果在另一种实现方式中,每个列都是单独分配的,则调用者将必须释放返回数组中的每个指针。

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

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