简体   繁体   English

指向函数中结构元素字段数组的指针

[英]Pointer to array of structs element field in a function

I have a function which searches for extrema in some data array. 我有一个在某些数据数组中搜索极值的函数。 It takes pointer to data, some parameters and a pointer to struct array where the result is stored. 它需要指向数据的指针,一些参数以及指向存储结果的struct数组的指针。 The function returns the length of the resulting struct array. 该函数返回结果结构数组的长度。

int FindPeaks(float *data, int WindowWidth, float threshold, struct result *p)
{
    int RightBorder = 0;
    int LeftBorder = 0;

    bool flag = 1;

    int i = 0;
    int k = 0;

    while(1)
    {
        flag = 1;
        if (WindowWidth >= 200) cout << "Your window is larger than the signal! << endl";
        if (i >= 200) break;
        if ((i + WindowWidth) < 200) RightBorder = i + WindowWidth;
        if ((i - WindowWidth) >= 0) LeftBorder = i - WindowWidth;
        for(int j = LeftBorder; j <= RightBorder; j ++)
        {
            if (*(data + i) < *(data + j))
            {
                flag = 0;
                break;
            }
        }
        if (flag && *(data + i) >= threshold && i != 0 && i != 199)
        {
            struct result pointer = p + k;
            pointer.amplitude = *(data + i);
            pointer.position = i;
            i = i + WindowWidth;
            k++;
        }
        else
        {
            i ++;
        }
    }

    return k;
}

I'm confused with a reference to i-th struct field to put the result in there. 我对第i个struct字段的引用感到困惑,无法将结果放入其中。 Am I doing something wrong? 难道我做错了什么?

You're trying to be too smart with use of pointers, so your code won't even compile. 您正在尝试使用指针来提高智能,因此您的代码甚至无法编译。

Instead of using *(data + i) or *(data+j) everywhere, use data[i] or data[j] . 不要在各处使用*(data + i)*(data+j) ,而要使用data[i]data[j] They're equivalent, and the second is often more readable when working with an array (assuming the data passed by the caller is actually (the address of the first element of) an array of float ). 它们是等效的,使用数组时,第二个通常更易读(假设调用者传递的data实际上是float数组(的第一个元素的地址))。

The problem you asked about is this code 您问的问题是此代码

struct result pointer = p + k;
pointer.amplitude = *(data + i);
pointer.position = i;

where p is a pointer to struct result passed to the function as argument. 其中p是指向作为struct result传递给函数的struct result的指针。 In this case, pointer actually needs to be a true pointer. 在这种情况下, pointer实际上需要是一个真实的指针。 Assuming you want it to point at p[k] (rather than creating a separate struct result ) you might need to do 假设您希望它指向p[k] (而不是创建单独的struct result ),则可能需要执行

struct result *pointer = p + k;    /*   equivalently &p[k] */
pointer->amplitude = data[i];
pointer->position = i;

This will get the code to compile. 这将使代码得以编译。 Note that you have not described what the function is actually supposed to achieve, so I have not bothered to check if the code actually does anything sensible. 请注意,您尚未描述该功能实际上应实现的功能,因此,我不必费心检查代码是否确实做了明智的事情。

Note that you're actually (mis)using C techniques in C++. 请注意,您实际上(错误地)使用C ++中的C技术。 There are much better alternatives in modern C++, such as using standard containers. 现代C ++中有更好的替代方法,例如使用标准容器。

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

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