简体   繁体   English

从C函数返回2D数组

[英]Returning a 2D array from c function

I'm a python programmer writing some c (wrapped in ctypes) to speed up some tight loops. 我是一名Python程序员,编写了一些c(包装在ctypes中)以加快一些紧密的循环。 I'm having trouble with a function that takes a two dimensional array (*double)[2] , does some things to it and returns it. 我在使用二维数组(*double)[2]的函数时遇到了麻烦,对它做了一些事情然后将其返回。 Function looks something like the following: 函数如下所示:

double *process(int n, double (*results)[2], ...) {
    // Do some things to results
    return results;
}

Here, n is the number of elements in the first level of results. 在此,n是第一级结果中的元素数。 This doesn't compile, apparently I'm returning from an incompatible pointer type. 这不会编译,显然我是从不兼容的指针类型返回的。 Making it return *results allows it to compile and run (all the contained logic is fine), but segfaults at the return. 使它return *results允许它编译和运行(所有包含的逻辑都可以),但是在返回时出现段错误。 Making the function a double ** and returning with any number of * s doesn't let me compile. 使函数成为double **并返回任意数量的* s不会让我编译。

Clearly I'm being an idiot here, which isn't surprising at all as I don't really understand c pointers. 显然我在这里是个白痴,这一点都不奇怪,因为我不太了解c指针。 What do I need to do to make this work? 我需要做些什么才能使这项工作? I've got another function that returns a double * working fine, so it's clearly something to do with the 2D array. 我还有另一个函数,它返回double *正常工作,因此显然与2D数组有关。

If you just want to get new results you even don't need to return anything. 如果您只是想获得新results ,甚至不需要返回任何内容。 The array is passed by address, not values. 数组按地址而不是值传递。

void process(int n, double results[][2], ...) {
    // Do some things to results,
    results[n][0] = 1.0l;
    // And no need to return.
}
int main(){
    double array[2][2] = { .0 };
    printf( "Before process: %lf", array[1][0] );
    process( 1, array );
    printf( "After process: %lf", array[1][0] );
}

It should output: 它应该输出:

Before process: 0.0000000000
After process: 1.0000000000

to return 2D array you need to use 返回二维数组,您需要使用

double **

To take a 2D array as parameter in function process() you can use 要将2D数组用作函数process()中的参数,可以使用

double *results[2]

or 要么

double results[2][]

Your function's return value expects only a 1D array as return value. 函数的返回值只需要一1D array作为返回值。 to make it work with 2D arrays, make it double ** . 要使其与2D数组一起使用,请将其double **

when you're returning, return only results . 返回时,仅返回results don't put any * in front of it. 不要在其前面加上任何*

I think you want this: 我想你想要这个:

double ** process(int n, double ** results)
{
    /* Do stuffz */
    return results;
}

You probably want to return a pointer to a two dimension array. 您可能想返回一个指向二维数组的指针。 In this case you would have to use: 在这种情况下,您将必须使用:

double (*process (int n, double results[][2], ...))[2] {
  return results;
}

This basically means "return a pointer to two 2 elements of double" 这基本上意味着“返回两个double的2个元素的指针”

Or, to make it somehow more readable: 或者,以某种方式使其更具可读性:

typedef double arrayType[2];

arrayType * process(int n, double results[][2]) {
    return results;
}

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

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