简体   繁体   中英

Converting the index of 1D array to access 2D array

void dgem(int n, double *A, double *B, double *C)
{

    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; j++){

            double cij = C[i+j*n]; /* cij = C[i][j] */

            for(int k = 0; k < n; k++){
                cij += A[i+k*n] * B[k+j*n]; /*cij = A[i][k]*b[k][j]*/
                C[i+j*n] = cij; /* C[i][j] = cij */
            }
        }
    }

}

This code is from Computer_Organization_and_Design_5th

Is it right? double cij = C[i+j*n];

As far as I know, it should be C[i*n + j]

int main(void){

    double A[4][4] = {1,2,3,4,
                      5,6,7,8,
                      9,10,11,12,
                      13,14,15,16};
    double * a = &A[0][0];

    int n = 4;
    printf("%f %f %f %f", *(*(A+1)+3), A[1][3], a[1*n + 3], a[1 + 3*n]); /* 
when i == 1 and j == 3 */

    return 0;

}

OUTPUT:

8.000000 8.000000 8.000000 14.000000

When I try with gcc, it doesn't make sense...

The below statement

double cij = C[i+j*n]; 

is correct. To understand this Lets assume double ptr[3] = {1.5,2.5,3.5] where ptr is array of three double variable. Now how will you access ptr[0] or ptr[1] etc.

 ----------------------------------------
|    1.5   |   2.5   |   3.5   |   4.5   |
-----------------------------------------
0x100     0x108    0x116     0x124     0x132 <-- lets say starting address 
LSB                                                 of ptr is 0x100
|
ptr

for row = 1

ptr[row] == *(ptr + row * sizeof(ptr[row]))
2.5      == *(0x100 + 1*8)
2.5      == *(0x108)
2.5      == 2.5

From above you can't have *(ptr*8 + row) it should be *(ptr + row*8) .

Similarly if ptr is 2D array like double ptr[2][3] then

ptr[row][col] == *( *(ptr + row) + col*8)

Similarly in your case valid one is C[i + j*n] not C[i*n +j]

Edit :- you have an 2D array like below

double A[4][4] = { {1,2,3,4} , {5,6,7,8} , {9,10,11,12} , {13,14,15,16} };

And

double *a = &A[0][0];

Now it looks like

    A[0]          |   A[1]        |   A[2]           |   A[3]             |
  -------------------------------------------------------------------------
  | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |  16 |
  -------------------------------------------------------------------------
0x100 0x108...................0x156.......................0x156 (assume that 0x100 is starting address of A)
  A
  a
 LSB -->  

Now when you do a[1*n + 3]) How internally its expanded

a[1*n + 3]) == *(a + (1*n + 3)  /*note that a is double pointer, it increments by 8 bytes
a[1*n + 3]) == *(0x100 + (1*4 + 3) *8)
a[1*n + 3]) == *(0x100 + 56)
            == *(0x156)
            ==  8 /* it prints 8.000000 */

And when you do a[1+3*n]) How internally its expanded

a[1+3*n]) == *(a + (1+3*n)*8)
a[1+3*n]) == *(0x100 + (1+3*4)*8)
a[1+3*n]) == *(0x100 + 96)
          == *(0x196)
          == 14 /* it prints 14.000000 */

When you do *(*(A+1) +3)) it internally expanded as

*(*(A+1) +3)) == *( *(0x100 +1*32) + 3*8) /* A+1 means increment by size of A[0] i.e 32 */ 
*(*(A+1) +3)) == *( *(0x132) + 24)
*(*(A+1) +3)) == *( 0x132 + 24 ) == *(156)
*(*(A+1) +3)) == 8 /*it prints 8.000000 */

And When you do A[1][3] which is same as above case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM