0

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.

Jason
  • 11,744
  • 3
  • 42
  • 46
Scott
  • 41
  • 4
  • 1
    The loop will iterate len amount of times, during this a variable called "v" which is of type double will be set to equal to whatever buff at i is. Then, variable "I" which is of type float is set to equal v + the dereferenced value of "In" when it is at i. Finally, we deference Pt to equal to our float variable "I" – SomeStudent Aug 11 '16 at 02:49
  • @Olaf There's a pointer to an array, so it's not all that far off. – 4castle Aug 11 '16 at 03:11
  • Possible duplicate of [Understanding pointers in C](http://stackoverflow.com/questions/10187510/understanding-pointers-in-c) – progyammer Aug 11 '16 at 04:10
  • @4castle: No, there is **no** pointer to an array! `float` is certainly not an array. – too honest for this site Aug 11 '16 at 12:01
  • @Olaf It's a pointer to the first element of an array. If you saw the calling scope, there would be an array. Of course this code is very fragile, because it doesn't enforce that. – 4castle Aug 11 '16 at 13:16
  • @4castle: Exactly, thus not a pointer to array! Sorry, my crystal ball is still in service (those fairies are really bad at this). How do you know there is an array behind those pointers? Please share your knowledge. – too honest for this site Aug 11 '16 at 13:17
  • @Olaf Because they have a constant integer length, and you can iterate over the values. The author also saw fit to use the `[]` syntax. – 4castle Aug 11 '16 at 13:19
  • @4castle: The index-**operator** does not indicate anything. It is exactly the same as `*(p + i)`, no matter what `p` actually points to. The operator does not even work on arrays, but pointers only. Without a [mcve] it is impossible from the given information to know if there is really an array somewhere. It is all your assumption. – too honest for this site Aug 11 '16 at 13:27
  • @Olaf Would any sane programmer use `[]` in a `for` loop when there wasn't an array? I think it's safe to make some assumptions. – 4castle Aug 11 '16 at 13:29
  • @4castle: Do you get the same question here I get? Maybe that's different for your tags, but for C, this is a typical problem. But languages with a double security net and padded armchair might be different. In that case, you might first learn C and about its pitfalls. – too honest for this site Aug 11 '16 at 13:58

1 Answers1

2

The function 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 the start of an area of memory. That memory can be considered to contain zero or more float values.
  • const float *In will contain an address to the start of an area of memory. That memory can also be considered to contain zero or more float values.
  • float *Pt will contain an address to the start of 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 - also refered to as a 'null pointer' (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 the start of an area or memory to store more than one item
  • be a value that points at an invalid memory location and should not be used

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

Jason
  • 11,744
  • 3
  • 42
  • 46
  • C does not support _methods_. Even iff, this is just a _function_. And a pointer cannot be "null", but a _null pointer_. It also never points to "an area of memory - how could it, it can only hold a single address. And a pointer holding an invalid value does not point anywhere. – too honest for this site Aug 11 '16 at 13:18
  • @Olaf They are using Java terminology to describe C, because the OP apparently understands Java better. "Understanding" a piece of code doesn't require a compiler-level amount of explanation. These are good analogies. – 4castle Aug 11 '16 at 13:48
  • @4castle: There is a great difference between methods and functions. If OP or the answerer does not understand this, he is quite lost anyway. Using incorrect terms definitively does not help. – too honest for this site Aug 11 '16 at 13:54
  • @Olaf I was a C programmer before moving to Java. I have edited to clarify some of the issues you raised. Hopefully this will help the OP and future readers. – Jason Aug 11 '16 at 22:52