Isn't double[][] equivalent to **double?

前端 未结 9 1492
心在旅途
心在旅途 2020-12-29 02:37

I\'m asking this because my program have two functions to multiply matrices, they multiply only 4x4 and 4x1 matrices. The headers are:

 double** mult4x1(doub         


        
9条回答
  •  醉酒成梦
    2020-12-29 02:54

    Although both double[N][M] and double** let you deal with 2D arrays, the two are definitely not equivalent: the former represents a 2D array of doubles, while the later represents a pointer to pointer to double, which can be interpreted (with the help of convenient square bracket syntax of C/C++) as an array of pointers to double, or as an array of arrays of double.

    The double[N][M] represents 2D arrays of "rectangular" shape, while double** lets you build "free-form" (jagged) arrays by allocating different amounts of memory to each row of your matrix.

    The difference to the compiler is that given double[N][M] and a pair of integers {r,c} it can compute the location of the element by computing the offset from the origin of the array, without reading anything from the memory. Given a double**, however, the compiler must compute the address of the pointer to a row, read that pointer, and only then compute the address of the target element. Because of this difference, the two are not interchangeable.

提交回复
热议问题