Reputation: 411
I have got this function written in C.
Are them two statements equivalent?
void dot_prod(float *A, int m, float *B, float *C) {
int i, j, z, k;
for (i = 0; i < m; i++) {
for (j = 0; j < m; j++) {
for (k = 0; k < m; k++) {
C[i * m + k] += A[i * m + j] * B[m * j + k];
//*(C + i * m + k) += *(A + i * m + j) * (*(B + m * j + k)); // is equivalent?
}
}
}
}
Is
C[i * m + k] += A[i * m + j] * B[m * j + k]
equivalent to
*(C + i * m + k) += *(A + i * m + j) * (*(B + m * j + k))
?
Upvotes: 0
Views: 95
Reputation: 76688
Almost.
C[i * m + k] += A[i * m + j] * B[m * j + k]
is equivalent to
*(C + (i * m + k)) += *(A + (i * m + j)) * (*(B + (m * j + k)))
This is different than without the extra parentheses, since the addition in the indices may overflow, in which case the pointer/integer addition is not necessarily associative, depending on the sizes of int
and pointers.
Upvotes: 3