As part of a question designed to help us understand the relationship between pointers and arrays in C, I've been asked to write an inner product function that doesn't use any array subscripting. Here's what I came up with, but it looks like the kind of complicated 'clever' coding that we've traditionally been told NOT to write. Any feedback on it, or how it could be done better / more efficiently would be much appreciated.
int inner_product(const int *a, const int *b, int n) { const int *p, *q; int result = 0; for(p = a, q = b; p < a + n, q < b + n; p++, q++) { result += *p * *q; } return result; }
Edit - Inner product is simply summing the product of the indexes, so a[1,2,3] b[2,3,4] would be 1*2 + 2*3 + 3*4