简体   繁体   English

C ++中2D数组的指针?

[英]Pointer of a 2D array in C++?

I am writing a function to rotate a NxN matrix by 90 degree. 我正在编写一个将NxN矩阵旋转90度的函数。

Here is my function 这是我的功能

// "matrix" is an n by n matrix  
void rotateMatrix(int ** matrix, int n)
{
    for(int layer=0; layer < n; layer++)
    {
        int first = layer, last = n - layer -1;
        for(int i=0; i<n; i++)
        {
            int temp = matrix[i][last];
            matrix[i][last] = matrix[first][i];
            matrix[first][i] = matrix[i][first];
            matrix[i][first] = matrix[last][i];
            matrix[last][i]=temp;
        }
    }
}

Here is how I initialize and call the function in main function: 这是我如何初始化和调用main函数中的函数的方法:

int m[5][5] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
rotateMatrix(m,5);

What I got from my IDE is: 我从IDE中得到的是:

> error: cannot convert ‘int (*)[5]’ to
> ‘int**’ for argument ‘1’ to ‘void
> rotateMatrix(int**, int)’

I kinda know why it's wrong since "m" is a int* . 我有点知道为什么错了,因为“ m”是一个int *。 However, I am not sure I can I solve this problem? 但是,我不确定是否可以解决此问题?

To actually solve your problem, assuming you're sticking with plain ol' arrays, and that the size needs to be arbitrary, you're going to have to make a couple of changes. 为了真正解决您的问题,假设您坚持使用简单的ol'数组,并且大小必须是任意的,则必须进行一些更改。

First, you need to pass in just a single pointer int* . 首先,您只需要传递一个指针int* A 2D array is not an array of pointers (or a pointer to a pointer), it's just a 1D array that the compiler knows to treat as a 2D array (ie the [3][4] element in a 5x5 array is at position 3*5+4). 2D数组不是指针(或指向指针的指针)数组,它只是编译器知道将其视为2D数组的1D数组(即5x5数组中的[3] [4]元素位于3 * 5 + 4)。 This does mean that you'll have to typecast the 2D array before passing it in, since the compiler doesn't want to discard that extra information. 这确实意味着您必须在传递2D数组之前对它进行类型转换,因为编译器不想丢弃这些额外的信息。

Second, you'll have to access it as a 1D array. 其次,您必须将其作为一维数组进行访问。 Like the example above, m[i][j] can be found at m[n*i+j] . 像上面的示例一样,可以在m[n*i+j]处找到m[i][j] m[n*i+j]

Edit, to address the downvotes: this answer is not intended as a full explanation of array and pointer typing. 编辑,以解决downvotes:这个答案是打算作为数组和指针的类型的完整说明。 It is simply trying to tell the OP a way to pass an arbitrary-sized 2D array into a function and perform some operation on it. 它只是试图告诉OP一种将任意大小的2D数组传递给函数并对其执行一些操作的方法。 I don't believe I said anything incorrect, just incomplete. 我不相信我说的任何错误,只是不完整。

Are the dimensions of the matrix known at compile time? 在编译时是否知道矩阵的尺寸? If so: 如果是这样的话:

template <size_t n>
void rotateMatrix(int (&matrix)[n][n])
{
    ...
}

An array is not exactly the same thing as a pointer. 数组与指针并不完全相同。 In particular, a pointer to an array is not a pointer to a pointer. 特别是,指向数组的指针不是指向指针的指针。 It's just a regular pointer to the first object of the array. 它只是指向数组第一个对象的常规指针。 An array of arrays likewise doesn't contain any pointers. 数组的数组同样不包含任何指针。

Much array confusion can be alleviated in C++ by using array references: 通过使用数组引用,可以在C ++中缓解很多数组混乱:

void rotateMatrix(int (&matrix)[5][5], int n) // matrix is ref to 5x5 array

Now there is no pointer vs array confusion whatsoever. 现在没有任何指针与数组的混淆。

You can keep your function the same, but you cannot pass static arrays in. You'll need to make dynamic arrays. 您可以保持函数不变,但是不能传入静态数组。您需要制作动态数组。

int size = 5;
int** m; 
m = new int**[size];
for (int i =0; i < size; ++i){
   m[i] = new int*[size];
}

/* Assign values to every element of m, using a for loop */
rotateMatrix(m, size);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM