Scott Scott - 3 months ago 11
C Question

Understanding portion of C code for Java conversion

I'd like to convert some code written in C into Java, however I'm having difficulties understanding this snippet:

void iqfir(float *buff, const float *In, float *Pt, const int len)
{
int i;
float I;

I = 1.0;

for (i = 0; i < len; i++) {
double v;
v = buff[i];
I += v * In[i];
} *Pt = I;
}


I'm having trouble understanding the use of pointers and arrays here.

Answer

The method is summing the result of multiplying each item in the buff array with the item in the In array at the same index.

For example, if buff was:

1.02 
2.22

and In was:

3.43
6.55

and len was 2, the answer would be:

(1.02 * 3.43) + (2.22 * 6.55)

Now to explain what each parameter is.

  • float *buff will contain an address to an area of memory. That memory can be considered to contain zero or more float values.
  • const float *In will contain an address to an area of memory. That memory can also be considered to contain zero or more float values.
  • float *Pt will contain an address to an area of memory. In this case, it is expected that this will point to one float value that the method is supposed to save the answer to.

In general, a pointer can:

  • be null (and therefore not point to any memory)
  • be a value that points at a single memory location to store one item
  • be a value that points at an area or memory to store more than one item
  • be a value that points at invalid memory and should not be used

In this case, both buff and In point to an area of memory containing a number of float values (hopefully at least as many as len specifies). Pt points to an area of memory that has been allocated so that the method can provide the result of the calculation to the caller.

Comments