如何将2D数组返回给C中的函数?

时间:2022-03-09 17:26:40

I am a Ruby programmer who has ended up developing a code generate for C. Its like asking a Limo to tow a 1960s truck. Any way.

我是一个Ruby程序员,最后为c开发了一个代码生成,这就像让一辆豪华轿车拖着一辆上世纪60年代的卡车。任何方式。

Here is what I thought should work but doesnt work.

这是我认为应该行得通但行不通的。

float[][] pixels()
{
  float x[][]= { {1,1},{2,2} };
  return x
}

void drawLine(float x[][2])
{
  //drawing the line
}

//inside main
drawLine(pixels());

I have banged my head on my desk trying to get this thing work. Please help.

我的头撞在桌子上,想把这东西修好。请帮助。

5 个解决方案

#1


24  

You poor thing. In C, pointers and arrays are closely related. Also, you usually need to pass the size of an array as a separate variable. Let's start you with:

你可怜的东西。在C语言中,指针和数组是密切相关的。此外,通常需要将数组的大小作为单独的变量传递。让我们开始你:

#include <stdio.h>

float** createArray(int m, int n)
{
    float* values = calloc(m*n, sizeof(float));
    float** rows = malloc(n*sizeof(float*));
    for (int i=0; i<n; ++i)
    {
        rows[i] = values + i*m;
    }
    return rows;
}

void destroyArray(float** arr)
{
    free(*arr);
    free(arr);
}

void drawLine(const float** coords, int m, int n);

int main(void)
{
    float** arr = createArray(2,2);
    arr[0][0] = 1;
    arr[0][1] = 1;
    arr[1][0] = 2;
    arr[1][1] = 2;
    drawLine(arr, 2, 2); 
    destroyArray(arr);
}

#2


4  

Thank you all for your answers and more specifically for the detailed explanation of the array-pointer relationship.

感谢大家的回答,更具体地说,感谢对array-pointer关系的详细解释。

I encapsulated the array in a structure

我将数组封装在一个结构中

 struct point_group1 {
        float x[3];
        float y[3];
};

struct point_group1 pixels(){
    struct point_group1 temp;

    temp.x[0] = 0.0;
    temp.x[1] = 1.0;
    temp.x[2] = -1.0;

    temp.y[0] = 0.0;
    temp.y[1] = 1.0;
    temp.y[2] = 1.0;

    return temp;    
}



struct point_group1 points1  = pixels();
axPoly(points1.x, points1.y ,3, 0.0);

#3


3  

In C/C++, when you pass an array to a function, it decays to be a pointer pointing to first element of the array. So, in pixels() function, you are returning the address of a stack allocated variable. The returning variable's address is no longer valid because on pixels() return, the stack allocated variable goes out of scope. So, instead you should for a variable whose storage is dynamic ( i.e., using malloc, calloc ).

在C/ c++中,当您将数组传递给函数时,它会衰减为指向数组第一个元素的指针。因此,在pixels()函数中,您将返回一个堆栈分配变量的地址。返回变量的地址不再有效,因为在pixels()返回时,堆栈分配的变量超出了范围。因此,对于存储是动态的变量(例如。,使用malloc, calloc)。

So, for a two dimensional array, you may use float** arrayVariable;. Also, if you passing this to a function, you should be wary of how many rows & columns it has.

因此,对于二维数组,可以使用float* arrayVariable;此外,如果您将它传递给一个函数,您应该注意它有多少行和列。

int rows, columns;

float** pixels()
{
    // take input for rows, columns
    // allocate memory from free store for the 2D array accordingly
    // return the array
}

void drawLine( float** returnedArrayVariable )
{
  //drawing the line
}

Since, 2D array is managing resources it self, it should return the resources back to the free store using free.

由于2D数组是自己管理资源的,它应该使用free返回资源到free store。

#4


3  

float (*pixels(void))[2] 
{
  static float x[2][2]= { {1,1},{2,2} };
  return x;
}

void drawLine(float (*x)[2])
{
  //drawing the line
  //x[0][0];
}

//inside main
drawLine(pixels());

#5


1  

The easiest way is probably going to be declaring the float array in main and having pixels just fill it:

最简单的方法可能是在main中声明浮点数组,然后用像素填充它:

#define PIXEL_X_SIZE 2
#define PIXEL_Y_SIZE 2

int pixels(float x[][PIXEL_X_SIZE], int len) {
    /* I don't know if you want the logic of this method to ever change,
       but this will be roughly equivalent to what you do above */
    if (len < PIXEL_Y_SIZE) {
        /* the length of the passed array is too small, abort */
        return -1;
    }

    x[0][0] = x[0][1] = 1;
    x[1][0] = x[1][1] = 2;
    return 0;
}

void drawLine(float x[][PIXEL_X_SIZE]) {
    /* this will work fine */
}

int main() {
    float pixel_array[PIXEL_Y_SIZE][PIXEL_X_SIZE];
    pixels(pixel_array, PIXEL_Y_SIZE);
    drawLine(pixel_array);
}

You can also use malloc and free and store your pixels on the heap, but if this is all the bigger the pixels array is going to be, there's really no need and it just adds additional complexity to make sure your memory always get properly allocated and freed.

您还可以使用malloc和free并将像素存储在堆上,但是如果像素数组的大小越大,那么就没有必要了,它只会增加额外的复杂性,以确保您的内存始终得到适当的分配和释放。

#1


24  

You poor thing. In C, pointers and arrays are closely related. Also, you usually need to pass the size of an array as a separate variable. Let's start you with:

你可怜的东西。在C语言中,指针和数组是密切相关的。此外,通常需要将数组的大小作为单独的变量传递。让我们开始你:

#include <stdio.h>

float** createArray(int m, int n)
{
    float* values = calloc(m*n, sizeof(float));
    float** rows = malloc(n*sizeof(float*));
    for (int i=0; i<n; ++i)
    {
        rows[i] = values + i*m;
    }
    return rows;
}

void destroyArray(float** arr)
{
    free(*arr);
    free(arr);
}

void drawLine(const float** coords, int m, int n);

int main(void)
{
    float** arr = createArray(2,2);
    arr[0][0] = 1;
    arr[0][1] = 1;
    arr[1][0] = 2;
    arr[1][1] = 2;
    drawLine(arr, 2, 2); 
    destroyArray(arr);
}

#2


4  

Thank you all for your answers and more specifically for the detailed explanation of the array-pointer relationship.

感谢大家的回答,更具体地说,感谢对array-pointer关系的详细解释。

I encapsulated the array in a structure

我将数组封装在一个结构中

 struct point_group1 {
        float x[3];
        float y[3];
};

struct point_group1 pixels(){
    struct point_group1 temp;

    temp.x[0] = 0.0;
    temp.x[1] = 1.0;
    temp.x[2] = -1.0;

    temp.y[0] = 0.0;
    temp.y[1] = 1.0;
    temp.y[2] = 1.0;

    return temp;    
}



struct point_group1 points1  = pixels();
axPoly(points1.x, points1.y ,3, 0.0);

#3


3  

In C/C++, when you pass an array to a function, it decays to be a pointer pointing to first element of the array. So, in pixels() function, you are returning the address of a stack allocated variable. The returning variable's address is no longer valid because on pixels() return, the stack allocated variable goes out of scope. So, instead you should for a variable whose storage is dynamic ( i.e., using malloc, calloc ).

在C/ c++中,当您将数组传递给函数时,它会衰减为指向数组第一个元素的指针。因此,在pixels()函数中,您将返回一个堆栈分配变量的地址。返回变量的地址不再有效,因为在pixels()返回时,堆栈分配的变量超出了范围。因此,对于存储是动态的变量(例如。,使用malloc, calloc)。

So, for a two dimensional array, you may use float** arrayVariable;. Also, if you passing this to a function, you should be wary of how many rows & columns it has.

因此,对于二维数组,可以使用float* arrayVariable;此外,如果您将它传递给一个函数,您应该注意它有多少行和列。

int rows, columns;

float** pixels()
{
    // take input for rows, columns
    // allocate memory from free store for the 2D array accordingly
    // return the array
}

void drawLine( float** returnedArrayVariable )
{
  //drawing the line
}

Since, 2D array is managing resources it self, it should return the resources back to the free store using free.

由于2D数组是自己管理资源的,它应该使用free返回资源到free store。

#4


3  

float (*pixels(void))[2] 
{
  static float x[2][2]= { {1,1},{2,2} };
  return x;
}

void drawLine(float (*x)[2])
{
  //drawing the line
  //x[0][0];
}

//inside main
drawLine(pixels());

#5


1  

The easiest way is probably going to be declaring the float array in main and having pixels just fill it:

最简单的方法可能是在main中声明浮点数组,然后用像素填充它:

#define PIXEL_X_SIZE 2
#define PIXEL_Y_SIZE 2

int pixels(float x[][PIXEL_X_SIZE], int len) {
    /* I don't know if you want the logic of this method to ever change,
       but this will be roughly equivalent to what you do above */
    if (len < PIXEL_Y_SIZE) {
        /* the length of the passed array is too small, abort */
        return -1;
    }

    x[0][0] = x[0][1] = 1;
    x[1][0] = x[1][1] = 2;
    return 0;
}

void drawLine(float x[][PIXEL_X_SIZE]) {
    /* this will work fine */
}

int main() {
    float pixel_array[PIXEL_Y_SIZE][PIXEL_X_SIZE];
    pixels(pixel_array, PIXEL_Y_SIZE);
    drawLine(pixel_array);
}

You can also use malloc and free and store your pixels on the heap, but if this is all the bigger the pixels array is going to be, there's really no need and it just adds additional complexity to make sure your memory always get properly allocated and freed.

您还可以使用malloc和free并将像素存储在堆上,但是如果像素数组的大小越大,那么就没有必要了,它只会增加额外的复杂性,以确保您的内存始终得到适当的分配和释放。