不是double [] []等同于** double? [英] Isn't double[][] equivalent to **double?

查看:182
本文介绍了不是double [] []等同于** double?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这是因为我的程序有两个函数来乘矩阵,他们只乘4x4和4x1矩阵。标头是:

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(double **m1, double **m2);
 double** mult4x4(double **m1, double **m2);

它们执行m1 * m2并返回一个** double,下面是一个4x4乘法。

They do m1*m2 and return it in a **double, below is a snippet of 4x4 multiplication.

 double** mult4x4(double **m1, double **m2){
      double** result = (double**) malloc(sizeof(double)*4);
      for (int i = 0; i < 4; i++) {
           result[i] = (double*) malloc(sizeof(double)*4);
      }
      ...multiply...
      return result;
 }

mult4x1和mult4x4之间的区别仅在于它们内部使用的索引。

The difference between mult4x1 and mult4x4 are only in the indexes used inside them.

我有这3个矩阵:

double m1[4][4] = {
    {2, 3, 5, 6},
    {9, 8, 1, 7},
    {5, 4, 3, 1},
    {7, 6, 1, 2}
};

double m2[4][4] = {
    {1, 0, 0, 0},
    {0, 1, 0, 0},
    {0, 0, 1, 0},
    {0, 0, 0, 1}
};

double m3[4][1] = {
    {2},
    {3},
    {3},
    {1}
};

尝试乘以这些矩阵时发生错误。

Trying to multiply these matrices an error occurs.

double** test = mult4x4(m1, m2);
double** test2 = mult4x1(identity4x4(), m3);
//identity4x4() creates a 4x4 identity matrix - double** identity4x4();

产生:


错误:无法将 1()的 double(*)[4]'转换为 double * $ c> double * mult4x4(double * ,double *)'

error: cannot convert double (*)[4]' todouble*' for argument 1' todouble* mult4x4(double*, double*)'

错误:无法转换 * * mult4x1(double *)[*] [*] [1]'到 double * ,double *)'

error: cannot convert double (*)[1]' todouble*' for argument 2' todouble* mult4x1(double*, double*)'

不是double [] []双?双数组数组。
欢迎任何澄清,误解和错误。

Isn't double[][] supposed to be equal to **double? An array of arrays of double. Any clarifications, misconceptions and errors are welcome.

推荐答案

否。

A double ** 是指向double( double * )的指针。

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

所以实际上应该这样创建(注意第一个malloc sizeof()中的额外*):

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 }

有4个缓冲区可容纳4个双精度连续。

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

虽然double [4] [4]是内存中的连续缓冲区,像这样:

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} }

这篇关于不是double [] []等同于** double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆