谁能解释一下如何从函数返回 C 中的二维数组?

发布于 2024-12-15 04:50:33 字数 68 浏览 0 评论 0原文

我是 C 新手,在学习过程中我想从函数返回一个二维数组,以便我可以在我的主程序中使用它。谁能用例子向我解释一下。提前致谢。

I am new to C and during my learning I want to return a two dimensional array from a function, so that I can use it in my main program. Can anyone explain me the same with example. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北座城市 2024-12-22 04:50:33

这取决于它如何实施。您可以仅使用一个一维数组,其中您知道每行(行)的长度,并且下一行紧接着上一行开始。或者,您可以有一个指向数组的指针数组。不过,额外的成本是您需要取消引用两个指针才能获取一个数据元素。

// 2D array of data, with just one array
char* get_2d_ex1(int rows, int cols) {
    int r, c, idx;
    char* p = malloc(rows*cols);

    for (r=0; r<rows; r++) {
        for (c=0; c<cols; c++) {
            idx = r*cols + c;   // this is key
            p[idx] = c;         // put the col# in its place, for example.
        }
     }
     return p;
}

It depends how it is implemented. You can either work with just a one-dimensional array where you know the length of each (row) and the next row begins immediately after the previous one. OR, you can have an array of pointers to arrays. The extra cost though is you need to de-reference two pointers to get to one element of data.

// 2D array of data, with just one array
char* get_2d_ex1(int rows, int cols) {
    int r, c, idx;
    char* p = malloc(rows*cols);

    for (r=0; r<rows; r++) {
        for (c=0; c<cols; c++) {
            idx = r*cols + c;   // this is key
            p[idx] = c;         // put the col# in its place, for example.
        }
     }
     return p;
}
早乙女 2024-12-22 04:50:33

将您的函数声明为返回一个指向指针的指针。如果我们使用 int 为例:

int **samplefunction() {
    int** retval = new int*[100];
    for (int i = 1; i < 100; i++) {
        retval[i] = new int[100];
    }
    // do stuff here to retval[i][j]
    return retval;
}

Declare your function as returning a pointer to a pointer. If we use int as an example:

int **samplefunction() {
    int** retval = new int*[100];
    for (int i = 1; i < 100; i++) {
        retval[i] = new int[100];
    }
    // do stuff here to retval[i][j]
    return retval;
}
如痴如狂 2024-12-22 04:50:33

以下是如何创建、操作和释放“二维数组”的示例:

#include <stdlib.h>

#define ROWS   5
#define COLS   10

int **create_2d_array(size_t rows, size_t cols)
{
   size_t i;   
   int **array = (int**)malloc(rows * sizeof(int*));
   for (i = 0; i < rows; i++)
      array[i] = (int*)malloc(cols * sizeof(int));

   return array;
}

void free_2d_array(int **array, size_t rows, size_t cols)
{
   size_t i;
   for (i = 0; i < rows; i++)
      free(array[i]);

   free(array);
}

int main(void)
{
   int **array2d = create_2d_array(ROWS, COLS);
   /* ... */
   array2d[3][4] = 5;
   /* ... */
   free_2d_array(array2d, ROWS, COLS);

   return 0;
}

要创建“二维数组”/矩阵,您所要做的就是创建一个动态指针数组(在此示例中) case int*) 的行/宽度的大小:

int **array = (int**)malloc(rows * sizeof(int*));

然后将每个指针设置为指向列/高度的大小的 int 的动态数组:

array[i] = (int*)malloc(cols * sizeof(int));

请注意,强制转换malloc 不是必需的,这只是我的一个习惯。

Here's an example of how you might create, manipulate and free a "2d array":

#include <stdlib.h>

#define ROWS   5
#define COLS   10

int **create_2d_array(size_t rows, size_t cols)
{
   size_t i;   
   int **array = (int**)malloc(rows * sizeof(int*));
   for (i = 0; i < rows; i++)
      array[i] = (int*)malloc(cols * sizeof(int));

   return array;
}

void free_2d_array(int **array, size_t rows, size_t cols)
{
   size_t i;
   for (i = 0; i < rows; i++)
      free(array[i]);

   free(array);
}

int main(void)
{
   int **array2d = create_2d_array(ROWS, COLS);
   /* ... */
   array2d[3][4] = 5;
   /* ... */
   free_2d_array(array2d, ROWS, COLS);

   return 0;
}

To create a "2d array"/matrix, all you have to do is create a dynamic array of pointers (in this case int*) of the size of the rows/width:

int **array = (int**)malloc(rows * sizeof(int*));

Then you set each of those pointers to point to a dynamic array of int of the size of the columns/height:

array[i] = (int*)malloc(cols * sizeof(int));

Note that the casts on malloc aren't required, it's just a habit I have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文