Home > Enterprise >  Pointer Arithmetic equivalence
Pointer Arithmetic equivalence

Time:12-23

I have got this function written in C.

Are they 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))?

CodePudding user response:

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.

  • Related