[英]Initializing and Navigating a char[][]
Let's say I have a char[2][3] named charPP, 2 rows, 3 columns. 假设我有一个名为charPP的char [2] [3],2行,3列。
reinterpret_cast<char**>(charPP[0])
? reinterpret_cast<char**>(charPP[0])
会发生什么? Sorry if this is generic or vague, I'm just looking for some basic understanding. 抱歉,这是通用的还是含糊的,我只是在寻找一些基本的知识。
As for case number 3, I guess you really means reinterpret_cast<char**>(charPP)
? 至于案例3,我猜你真的是说
reinterpret_cast<char**>(charPP)
吗? Well you simply can't access an array of arrays as a pointer to pointer, because the memory layout is not compatible. 好吧,您根本无法将数组的数组作为指向指针的指针,因为内存布局不兼容。
Lets say you have an array char a[2][2]
, it's memory layout would be 假设您有一个数组
char a[2][2]
,它的内存布局为
+---------+---------+---------+---------+ | a[0][0] | a[0][1] | a[1][0] | a[1][1] | +---------+---------+---------+---------+
However, if you use pointer to pointer (ie char **
), which you used as the above array, it would look like this: 但是,如果您使用了用作上述数组的指针(即
char **
),则它看起来像这样:
+------+------+-----+ | a[0] | a[1] | ... | +------+------+-----+ | | | V | +---------+---------+---------+-----+ | | a[1][0] | a[1][1] | a[1][2] | ... | | ----------+---------+---------+-----+ V +---------+---------+---------+-----+ | a[0][0] | a[0][1] | a[0][2] | ... | +---------+---------+---------+-----+
For case number 2, while it's true that you can use an array as a pointer (arrays decays to pointers), it's still an array so you can't make parts of it "point" to null. 对于案例2,确实可以使用数组作为指针(数组会衰减为指针),但它仍然是数组,因此您无法将其中的一部分“指向”为null。 Lets take the example above, with
char a[2][2]
, you can't do a[0] = nullptr
. 让我们以上面的示例为例,对于
char a[2][2]
,您不能执行a[0] = nullptr
。
In case number 1, you do know how to access a single cell for a simple (one-dimensional) array don't you? 在案例1中,您确实知道如何访问简单(一维)数组的单个单元格吗? It's just the same for multi-dimensional arrays, as long as you remember that you have an array of arrays .
你还记得你有一个数组的数组这只是对多维数组一样,只要。
char charPP[2][3] = { 0 };
charPP[i][j] = 0;
char[0][2] = 0; char[1][2] = 0;
Possible mayhem since you're casting a char value(-128 to 127) to a char**
. 由于您正在将char值(-128到127)强制转换为
char**
可能会造成混乱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.