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

前端 未结 9 1398
心在旅途
心在旅途 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:48

    No.
    A double** is a pointer to a pointer to a double (double*).

    So actually it should be created like this (note the extra * in the first malloc sizeof()):

      double** result = (double**) malloc(sizeof(double*)*4);
      for (int i = 0; i < 4; i++) {
           result[i] = (double*) malloc(sizeof(double)*4);
      }
    

    So in memory it would look like this:

    [] -> { d,d,d,d }
    [] -> { d,d,d,d }
    [] -> { d,d,d,d }
    [] -> { d,d,d,d }
    

    There are 4 buffers that hold 4 doubles, but are not continuous.

    While your double[4][4] is a continuous buffer in memory, like this:

     { { d,d,d,d } { d,d,d,d } {d,d,d,d} {d,d,d,d} }
    

提交回复
热议问题