从C中的函数返回多维数组 [英] Returning multidimensional arrays from a function in C

查看:205
本文介绍了从C中的函数返回多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从c中的函数返回多维数组的最佳方式是什么?

What is the best way to return a multidimensional array from a function in c ?

假设我们需要在函数中生成一个多维数组并在主函数中调用它,最好是将它包装在一个结构中,或者只是返回一个指向堆内存的指针?

Say we need to generate a multidimensional array in a function and call it in main, is it best to wrap it in a struct or just return a pointer to memory on the heap ?

 int *create_array(int rows, int columns){
     int array[rows][columns] = {0};
     return array;
 }

 int main(){

     int row = 10;
     int columns = 2;
     create_array(row,columns); 
 }

上面的代码只是简单介绍了我所想到的基本程序。

The code above, is just to sketch out the basic program I have in mind.

推荐答案

这是错误的:

This is wrong:

int *create_array(int rows, int columns){
     int array[rows][columns] = {0};
     return array;
}

并产生如下警告:

and should produce a warning like this:

prog.c:2:6: note: (near initialization for 'array')
prog.c:3:13: warning: return from incompatible pointer type [-Wincompatible-pointer-types]
      return array;
             ^~~~~
prog.c:3:13: warning: function returns address of local variable [-Wreturn-local-addr]

,因为您正在返回一个自动变量的地址;它的生命周期在其相应的函数终止时结束。

since you are returning the address of an automatic variable; its lifetime ends when its corresponding function terminates.

您应该在 main中声明一个双指针(),将它传递给函数,为它动态分配内存并返回该指针。或者您可以在 main()中创建数组,并将双指针传递给该函数。




You should either declare a double pointer in main(), pass it through the function, dynamically allocate memory for it and return that pointer. Or you could create the array in main() and pass the double pointer to the function.


我想知道如何在堆上分配多维数组并将它们传递给

I want to know ways to allocate multidimensional arrays on the heap and pass them around

为了在堆上分配内存,您可以使用以下两种方法之一,它们涉及指针

For allocating memory on the heap you could use one of these two methods, which involve pointers:

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

// We return the pointer
int **get(int N, int M) /* Allocate the array */
{
    /* Check if allocation succeeded. (check for NULL pointer) */
    int i, **array;
    array = malloc(N*sizeof(int *));
    for(i = 0 ; i < N ; i++)
        array[i] = malloc( M*sizeof(int) );
    return array;
}

// We don't return the pointer
void getNoReturn(int*** array, int N, int M) {
    /* Check if allocation succeeded. (check for NULL pointer) */
    int i;
    *array = malloc(N*sizeof(int *));
    for(i = 0 ; i < N ; i++)
        (*array)[i] = malloc( M*sizeof(int) );
}

void fill(int** p, int N, int M) {
    int i, j;
    for(i = 0 ; i < N ; i++)
        for(j = 0 ; j < M ; j++)
            p[i][j] = j;
}

void print(int** p, int N, int M) {
    int i, j;
    for(i = 0 ; i < N ; i++)
        for(j = 0 ; j < M ; j++)
            printf("array[%d][%d] = %d\n", i, j, p[i][j]);
}

void freeArray(int** p, int N) {
    int i;
    for(i = 0 ; i < N ; i++)
        free(p[i]);
    free(p);
}

int main(void)
{
    int **p;
    //getNoReturn(&p, 2, 5);
    p = get(2, 5);
    fill(p ,2, 5);
    print(p, 2, 5);
    freeArray(p ,2);
    return 0;
}

挑选最适合您的款式。

这篇关于从C中的函数返回多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆