C ++中的多维数组和指针? [英] Multi-dimensional array and pointers in C++?

查看:119
本文介绍了C ++中的多维数组和指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int *x = new int[5]();

根据上述想法,应如何为二维数组写入代码 - int [] []

With the above mentality, how should the code be written for a 2-dimensional array - int[][]?

int **x = new int[5][5] () //cannot convert from 'int (*)[5]' to 'int **'


$ b b

在第一个语句中,我可以使用:

In the first statement I can use:

x[0]= 1;

但第二个更复杂,我无法想象。
我应该使用如下:

But the second is more complex and I could not figure it out. Should I use something like:

x[0][1] = 1;

或者,计算实际位置,然后为第四行和第1列获取值

Or, calculate the real position then get the value for the fourth row and column 1

x[4*5+1] = 1;


推荐答案

您可以分别进行初始化:

You can do the initializations separately:

int **x = new int*[5];
for(unsigned int i = 0; i < 5; i++)
    x[i] = new int[5];

这篇关于C ++中的多维数组和指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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