C语言 C - 指向矩阵的指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14450123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:06:44  来源:igfitidea点击:

C - Pointer to a matrix

cpointersmatrix

提问by Pantelis Sopasakis

I am trying to pass a matrix to a function by reference. The function will replace every element A[i][j]of the matrix by -A[i][j]. I first create the matrix:

我试图通过引用将矩阵传递给函数。该函数将用 替换A[i][j]矩阵的每个元素-A[i][j]。我首先创建矩阵:

float a[3][4] =
{
    {1.0f, 0.0f, 0.0f, 0.0f},
    {0.0f, 1.0f, 0.0f, 0.0f},
    {1.0f, 1.0f, 0.0f, 0.0f},
};

Then, I obtain the pointer to this matrix:

然后,我获得指向这个矩阵的指针:

float*** pa = &a;

Then, I introduce the following function:

然后,我介绍以下功能:

void process(float ***matrix, int nRows, int nCols){
 short i;
 short j;
 for (i=0 ; i<nRows; i++){
   for (j=0 ; j<nCols ; j++){
     (*matrix)[i][j] *= -1;
   }
 }
}

which I call as follows:

我称之为:

process(pa,3,4);

My program fails to execute and returns:

我的程序无法执行并返回:

Segmentation fault: 11

Any ideas?

有任何想法吗?

Summary of the answers:Some notes based on the questions this question received:

答案摘要:基于此问题收到的问题的一些注释:

I. The aforementioned function can be used, provided that ais initialized a bit differently so as to be a float**. In particular:

I. 可以使用上述函数,前提是a它的初始化方式稍有不同,以便成为float**. 特别是:

int numberOfRows = 3;
int numberOfColumns = 4;
float **a = (float **) malloc(sizeof (float *) * numberOfRows);
for (i = 0; i < numberOfRows; ++i) {
    a[i] = (float *) malloc(sizeof (float) * numberOfColumns);
}

and then, it is passed to the function processas process(&a, 3,4);.

然后,它被传递给函数processprocess(&a, 3,4);

II. Alternatively, one may use the function:

二、或者,可以使用以下功能:

void multi_by_minus(float *matrix, int nRows, int nCols) {
  short i,j;
  for (i = 0; i < nRows; i++) {
      for (j = 0; j < nCols; j++) {
          matrix[i * nCols + j] *= -1;
      }
  }
}

which treats the matrix as an one-dimensional array. In that case we simply invoke it as multi_by_minus(&a, 3, 4);

它将矩阵视为一维数组。在这种情况下,我们只需将其调用为multi_by_minus(&a, 3, 4);

III. Finally, we may use the method:

三、最后,我们可以使用以下方法:

void process2(int nRows, int nCols, float (*matrix)[nCols]) {
  short i, j;
  for (i = 0; i < nRows; i++) {
    for (j = 0; j < nCols; j++) {
        matrix[i][j] *= -1;
    }
  }
}

to which we provide a pointer to a, i.e., we invoke it like process2(3,4,&a);. In this way, we acquire access to the elements of the matrix in 2D.

我们提供一个指向 的指针a,即我们像 一样调用它process2(3,4,&a);。通过这种方式,我们可以访问二维矩阵的元素。

回答by imreal

There is no need for the triple pointer since you are already supplying the memory. You would use that if you were to allocate the memory inside de function.

由于您已经在提供内存,因此不需要三重指针。如果要在 de 函数内部分配内存,则可以使用它。

You can't index a 2 dimension matrix without supplying at least the size of 1 dimension. The reason is that the compiler needs to generate code to access the correct offset taking into account both dimensions. In this particular case, I suggest passing a simple pointer and indexing as a 1D array, like this:

如果不提供至少 1 维的大小,就不能索引 2 维矩阵。原因是编译器需要生成代码来访问正确的偏移量,同时考虑到两个维度。在这种特殊情况下,我建议传递一个简单的指针并作为一维数组进行索引,如下所示:

void process(float *matrix, int nRows, int nCols){
 short i;
 short j;
 for (i=0 ; i<nRows; i++){
   for (j=0 ; j<nCols ; j++){
     matrix[i * nCols + j] *= -1;
   }
 }
}

You can then call it like this:

然后你可以这样称呼它:

process((float*)a,3,4);

This way you manually index your buffer.

这样您就可以手动索引缓冲区。

回答by A.B.

You have to change the signature of the function to this:

您必须将函数的签名更改为:

void process(float (*matrix)[3][4], int nRows, int nCols){

And when calling the function, use this:

在调用函数时,使用这个:

process(&a, 3, 4);

回答by Eric Postpischil

If you put the nColsparameter before the matrixparameter, you can pass the two-dimensional matrix and use it in the natural way, without extra *operators or index arithmetic:

如果将nCols参数放在参数之前matrix,则可以传递二维矩阵并以自然的方式使用它,无需额外的*运算符或索引算法:

void process(int nRows, int nCols, float (*matrix)[nCols])
{
    for (short i = 0 ; i < nRows; i++)
    {
        for (short j = 0; j < nCols; j++)
        {
            matrix[i][j] *= -1;
        }
     }
}

Then you call processlike this:

然后你这样调用process

process(3, 4, matrix);

Incidentally:

顺便:

  • Unless there is special reason for making iand jshort, you should declare them int. intis defined to be the “natural” size for integers on the target platform.
  • 除非有特殊原因制作ijshort,您应该申报它们intint被定义为目标平台上整数的“自然”大小。

回答by milcom

So easy, if you have a matrix:

很简单,如果你有一个矩阵:

int m[2][2]={{1,0},{0,1}};

and you want to define a pointer to m, so you must declare:

并且你想定义一个指向 m 的指针,所以你必须声明:

int (*mptr)[2][2];
mprt=m; // or mptr=&m;  is the same.

and you can use it to point to elements of the matrix m.

你可以用它来指向矩阵 m 的元素。

(*mptr)[i][j]....