提问者:小点点

指向多维数组的指针C++


我试图理解如何使用指针与多维数组(例如:2维,3维。。。)。 我在网上看到了很多关于它的资源,但我似乎还是不能理解它。 语法也让我感到困惑。 下面这些语法是什么意思(为什么我们需要括号)? 代码是做什么的,它是如何工作的? 谢谢!

实施例1

int (*array)[5] = new int[10][5];

实施例2

int c[3][2][2]; 
int (*p)[2][2] = c; 

共2个答案

匿名用户

  1. int(*arr)[5]:arr是指针,指向5 int数组
  2. int(*p)[2][2]=C; :P是一个指针,指向一个2的数组,每一行都有一个2int
  3. 的数组

它是怎么工作的? 看下面这个简单的例子:

    int (*array)[2] = new int[3][2];

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 2; j++)
            array[i][j] = i + j;
    }
    
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 2; j++)
            cout << array[i][j] << ' ';
        cout << '\n';
    }

输出:

0 1 
1 2 
2 3 

考虑:

int *arr[3];

这里,arr是一个大小为3的数组,它可以存储3个指针。 所以它是一个指针数组。 参见:

int a = 10;
int b = 20;
int c = 30;
arr[0] = &a; //arr[0] pointing to a
arr[1] = &b; //arr[1] pointing to b
arr[2] = &c; //arr[2] pointing to c

匿名用户

int (*array)[5] -> declare array as pointer to array 5 of int

int c[3][2][2]  -> declare c as array 3 of array 2 of array 2 of int

int (*p)[2][2]  -> declare p as pointer to array 2 of array 2 of int

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(指向|多维|数组|指针|c++)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?