0

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. 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. Am I doing something wrong?

8
  • "I'm confused with a reference to i-th struct field to put the result in there. " - What do you mean by that? Commented Sep 5, 2016 at 11:32
  • struct result pointer = p + k;:, is have a feeling it should be: struct result* pointer = p + k; Commented Sep 5, 2016 at 11:33
  • I cannot compile this line. Troubles with setting pointer to i-th amplitude: pointer.amplitude = *(data + i); Commented Sep 5, 2016 at 11:34
  • 4
    "a pointer to struct array" Ew this is all very C-like. You're even using the struct prefix. What resource are you using to learn C++? Commented Sep 5, 2016 at 11:38
  • @Dankevich: We know you "cannot compile this line". But repeating the vague problem statement doesn't help.. Commented Sep 5, 2016 at 11:39

1 Answer 1

1

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]. 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).

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. In this case, pointer actually needs to be a true pointer. Assuming you want it to point at p[k] (rather than creating a separate struct result) you might need to do

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++. There are much better alternatives in modern C++, such as using standard containers.

Sign up to request clarification or add additional context in comments.

7 Comments

I dunno about "too smart".
@StoryTeller - I said "trying to be too start", not "being too smart". The meaning is totally different.
struct result sounds really C-like.
@Peter, I realize now that sarcasm is clearly not meant for this medium.
That's really hard to explain in full, without writing an essay (which is not suited to this format), Dankevich. However, in the declaration struct result *pointer = p+k, the struct keyword is not needed at all in C++ (but is in C). It's one of the reasons others have commented that your code is C and not C++. Additionally, in modern C++, you would rarely (possibly never) need to use a pointer or an array at all in ANY of your code .... if you learn about C++ standard containers like vector.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.