如何解决警告' double *'间接水平不同于双倍(*)[4] [4]'

时间:2018-02-19 13:22:58

标签: c pointers matrix

我正在尝试使用下载的tqli算法计算矩阵的特征值。作为一个简单的例子,我试图测试它的4x4单位矩阵:

double ze[4][4];
double *zepointer;
ze[0][0] = 1;
ze[1][1] = 1;
ze[2][2] = 1;
ze[3][3] = 1;

ze[0][1] = 0;
ze[1][0] = 0;
ze[0][2] = 0;
ze[2][0] = 0;

ze[0][3] = 0;
ze[3][0] = 0;
ze[1][2] = 0;
ze[2][1] = 0;

ze[1][3] = 0;
ze[3][1] = 0;
ze[2][3] = 0;
ze[3][2] = 0;

zepointer = &ze;

tqli(de,ee,4,zepointer);

除此之外,我希望有一种更聪明的方法来生成一个单位矩阵,我的编译器会给我一个警告说“双'间接水平不同于双重()[4] [4]'对于该行' zepointer =& ze;'。我查看this question并在那里说警告来自于在宣布之前的引用。但是,我无法看到我在代码片段中完成了这项工作。

TQLI是一种计算特征值和特征向量的算法。我是从here下载的。

2 个答案:

答案 0 :(得分:0)

  

我正在尝试使用下载的方法计算矩阵的特征值   tqli算法

尝试投射double**无法获得double[4][4]指针。

您必须使用不同的方法为double **函数提供所需的tqli指针。

您可以使用以下方法之一传递数据。

第一种方法:

1)为指针double **aa的指针分配内存,并使用指向a数组中行向量的指针初始化它们。

第二种方法:

2)为指向矩阵行的指针分配内存(如上所述)。为行的内容分配内存。复制ze

中的所有元素值

测试程序:

#include <stdlib.h>
#include <stdio.h>

#define M_SIZE  4  // size of the nxn matrix

void tqli(double d[], double e[], int n, double **z)
{
    int i,j;

    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
             double d = z[i][j]; 
             printf(" z[%i][%i] = %f \n", i, j, d );
        }
        printf("\n"); 
    }
}

int main()
{
    int i,j;

    double ze[M_SIZE][M_SIZE];
    double **zepointer;

    ze[0][0] = 1;
    ze[1][1] = 1;
    ze[2][2] = 1;
    ze[3][3] = 1;

    ze[0][1] = 0;
    ze[1][0] = 0;
    ze[0][2] = 0;
    ze[2][0] = 0;

    ze[0][3] = 0;
    ze[3][0] = 0;
    ze[1][2] = 0;
    ze[2][1] = 0;

    ze[1][3] = 0;
    ze[3][1] = 0;
    ze[2][3] = 0;
    ze[3][2] = 0;

    double de[M_SIZE] = {0};
    double ee[M_SIZE] = {0};

   // 1.---------------------

    printf(" First array is { {6,7,8,9}, {1,6,1,5}, {6,2,2,4}, {1,2,3,4} } \n"); 

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

    double **aa = malloc( M_SIZE * sizeof(double*)); // allocate memory to hold the pointers to all rows

    for(i=0; i<M_SIZE; i++) 
        aa[i] = a[i];     // initialize pointers to the `a` rows

    tqli(de,ee,M_SIZE,aa); // test

    free(aa);              // free memory for row pointers


   // 2.--------------------

   printf(" Second array is { {1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1} } \n"); 

   zepointer = malloc( M_SIZE * sizeof(double*));    // allocate memory to hold the pointers to all rows

   // allocate memory for every row to hold column elements
   for(i=0; i<M_SIZE; i++) 
      zepointer[i] = malloc (M_SIZE * sizeof (double )) ;

   // assign values from your ze[][] matrix 
   for(i=0; i<M_SIZE; i++) 
      for(j=0; j<M_SIZE; j++)
            zepointer[i][j] = ze[i][j];

   tqli(de,ee,M_SIZE,zepointer); // test

   // cleanup

   for(i=0; i<M_SIZE; i++)       // free memory allocated for row elements 
     free(zepointer[i]) ;

   free(zepointer);              // free memory for the row pointers      

   return 0;
}

输出:

 First method: array is { {6,7,8,9}, {1,6,1,5}, {6,2,2,4}, {1,2,3,4} } 
 z[0][0] = 6.000000 
 z[0][1] = 7.000000 
 z[0][2] = 8.000000 
 z[0][3] = 9.000000 

 z[1][0] = 1.000000 
 z[1][1] = 6.000000 
 z[1][2] = 1.000000 
 z[1][3] = 5.000000 

 z[2][0] = 6.000000 
 z[2][1] = 2.000000 
 z[2][2] = 2.000000 
 z[2][3] = 4.000000 

 z[3][0] = 1.000000 
 z[3][1] = 2.000000 
 z[3][2] = 3.000000 
 z[3][3] = 4.000000 

 Second method: array is { {1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1} } 
 z[0][0] = 1.000000 
 z[0][1] = 0.000000 
 z[0][2] = 0.000000 
 z[0][3] = 0.000000 

 z[1][0] = 0.000000 
 z[1][1] = 1.000000 
 z[1][2] = 0.000000 
 z[1][3] = 0.000000 

 z[2][0] = 0.000000 
 z[2][1] = 0.000000 
 z[2][2] = 1.000000 
 z[2][3] = 0.000000 

 z[3][0] = 0.000000 
 z[3][1] = 0.000000 
 z[3][2] = 0.000000 
 z[3][3] = 1.000000 

答案 1 :(得分:0)

从函数的定义

void tqli(double d[], double e[], int n, double **z)

我得出结论,函数期望double的2维被传递为“锯齿状”,即“散乱”数组。

要创建这样的东西,请执行以下操作:

const size_t rows = 4;
const size_t columns = 4;

/* Allocate as many pointers to column's 1st element as we have rows. */
double ** z = malloc(rows * sizeof *z);

/* Loop over all rows (pointers to column's 1st element). */
for (size_t r = 0; r < rows; ++r)
{
  /* Allocate to each row (pointer to column's 1st element) as many doubles as we have columns. */
  z[r] = malloc(columns * sizeof *z[r]);
}


/* Assign value to z's elements. */
for (size_t r = 0; r < rows; ++r)
{
  for (size_t c = 0; c < columns; ++c)
  {
    z[r][c] = r * c;
  }
}

tqli(..., z);

/* Free the stuff, 
   by calling free() on every item we received from malloc().  */
for (size_t i = 0; i < rows; ++i)
{
  free(z[i]);
}

free(z);

添加正确的错误检查留给读者。