2
\$\begingroup\$

I'm trying implement a fairy basic filter, and right now I have just some simple test code for me to enter in values. It errors out on the line when it gets to where I call the function I have written. I am fairly certain my error is coming from how I am using the pointers but I just don't understand pointers well enough to know what I'm doing wrong. Any thoughts on what I am doing wrong?

If it helps to understand what the filter should be doing: when a new value is input it is added to the to the buffer and the old state of what the average of the buffer was at is subtracted from the the buffer. the new average of the buffer is returned.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,a_buffer,b_buffer,valueToPrinta,valueToPrintb;
    printf("a value: ");
    scanf("%d",&a);
    printf("b value: ");
    scanf("%d",&b);

    valueToPrinta = integrate_filter_data_16bit(a, a_buffer, 2);
    valueToPrintb = integrate_filter_data_16bit(b, b_buffer, 2);

    printf("%d and %d", a_buffer,b_buffer);

    return 0;
}

int integrate_filter_data_16bit(int integrator_buffer_16bit, volatile int *pintegrator_accumulator16bit, int integrator_size_16bit)
{
  int temp_integrator_accumulator_16bit = *pintegrator_accumulator16bit;

  /* compute the new value of the IIR filter accumulator */
  *pintegrator_accumulator16bit = (int)integrator_buffer_16bit + (temp_integrator_accumulator_16bit - (temp_integrator_accumulator_16bit >> integrator_size_16bit));

  return((int)(*pintegrator_accumulator16bit >> integrator_size_16bit));
}

\$\endgroup\$
5
  • \$\begingroup\$ Also please note I haven't add a loop yet for this filter to take in multiple inputs \$\endgroup\$ Commented Jul 1, 2019 at 22:53
  • 2
    \$\begingroup\$ What do you mean "it errors out"? Are you getting an error from the compiler? If so, please show us the exact and entire error message. Also, please format your code properly. \$\endgroup\$ Commented Jul 2, 2019 at 0:26
  • \$\begingroup\$ @ElliotAlderson it causes a segmentation error and crashes, when you run the OP's code, e.g. using ideone.com... it at least doesn't do that when you do what I say in my answer. \$\endgroup\$
    – vicatcu
    Commented Jul 2, 2019 at 1:39
  • \$\begingroup\$ If you had declared a function prototype (not a cure all, but they can help) at the top of the file, you should have seen a compiler warning (incompatible types). \$\endgroup\$ Commented Jul 2, 2019 at 9:47
  • \$\begingroup\$ Why are you using volatile int * instead of passing by value? All this does is to slow down the program. \$\endgroup\$
    – Lundin
    Commented Jul 3, 2019 at 8:25

1 Answer 1

2
\$\begingroup\$

I think your problem is simple.

If your function accepts a pointer, you should use the & operator on the variable you pass to the function. In your case, the lines that say:

valueToPrinta = integrate_filter_data_16bit(a, a_buffer, 2);
valueToPrintb = integrate_filter_data_16bit(b, b_buffer, 2);

... should instead say...

valueToPrinta = integrate_filter_data_16bit(a, &a_buffer, 2);
valueToPrintb = integrate_filter_data_16bit(b, &b_buffer, 2);

Or alternatively you could change your variable declrations from:

int a,b,a_buffer,b_buffer,valueToPrinta,valueToPrintb;

... to:

int a,b,valueToPrinta,valueToPrintb;
int a_buffer[1],b_buffer[1];

... because then a_buffer and b_buffer, as symbols, are implicitly pointer variables equivalent to &(a_buffer[0]) and &(b_buffer[0]) respectively.

That being said, your printf's make no sense. If you put & before an expression, you get the address of the memory location allocated to the evaluation of that expression. If you don't put an & before an expression, you get the value stored at the memory location allocated to that expression. That's all there is to it.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.