简体   繁体   English

从 Objective-c 到双数组 C 代码 function 参数

[英]from Objective-c to double array C Code function argument

Hi I am getting a problem when passing an argument double array from Objective-c to C Code somehow when receiving to c code my data is not an array anymore.嗨,我在将参数双数组从 Objective-c 传递到 C 代码时遇到问题,当接收到 c 代码时,我的数据不再是数组了。 can you point out what is wrong with my code你能指出我的代码有什么问题吗

objective-c objective-c 目标c代码

C code C代码c代码

Sorry for a newbie question, I am still new to C Code对不起一个新手问题,我还是 C 代码的新手

Those are the same.那些是一样的。 On the Objective-C side it is shown as a double array and on the C side as a pointer to double, which is the same.在 Objective-C 一侧显示为双精度数组,在 C 一侧显示为双精度指针,相同。 Note how the first value in the array is the same as the first value pointed to by the pointer.请注意数组中的第一个值如何与指针指向的第一个值相同。

You can use eg你可以使用例如

double value = * ( data + i );

to get the value pointed to at index i from the array.从数组中获取指向索引i的值。

Also, change the arguments of the C function as follows.此外,将 C function 的 arguments 更改如下。

int classify_data( double * data, int size )

and pass the data in as now and also the size of the array.并像现在一样传递数据以及数组的大小。

EDIT编辑

FWIW the code below hopefully clarifies this similarity. FWIW 下面的代码有望澄清这种相似性。

        double data[] = { 1.0, 22222.0, 3.0 };
        double * p = data;

        // All print the same 22222
        NSLog(@"Some values %f %f %f %f", * ( data + 1 ), data[ 1 ], * ( p + 1 ), p[ 1 ] );

If you work with the pointer you can loop using pointer arithmetic which can be really nice and efficient, but that is a topic on its own.如果您使用指针,您可以使用指针算法进行循环,这非常好且高效,但这本身就是一个主题。 The pointer and the array can be used interchangeably as shown in the code because, in C, they are the same.如代码所示,指针和数组可以互换使用,因为在 C 中,它们是相同的。

EDIT 2 A sample of how you could loop a double array extremely efficiently.编辑 2如何非常有效地循环双数组的示例。

double sum ( double * data, int n )
{
    double sum = 0;

    while ( n )
    {
        // The more traditional way of doing it
        sum += * data;
        data ++;

        // Here you can even write a one-liner as below which is extremely efficient
        // This line below is a real nice example of how powerful pointer arithmetic is ...
        // sum += * ( data ++ );

        n --;
    }

    return sum;
}

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

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