如何在没有 [] 运算符的情况下正确取消引用多维数组
所以,我已经弄清楚如何取消引用多维数组,如下所示:
#include <tchar.h>
#include <iostream>
wchar_t g_pppwcSymbol[2][3][4];
int main(int argc, TCHAR **argv)
{
(* (*( (*(g_pppwcSymbol+1) )+2 )+3 ) ) = L'K';
std::wcout<<g_pppwcSymbol[1][2][3];
std::wcin.get();
return 0;
}
输出:K
但是,我也听说编译器将其转换为一维数组,这是正确的吗?这意味着所有 wchar_t 元素在内存中相互跟随。所以必须有一种方法可以做到这样的事情:
***(g_pppwcSymbol + x) = value;
但是我不确定它到底是如何工作的。有人可以详细说明一下吗?
编辑:
到目前为止这似乎有效:
int main(int argc, TCHAR **argv)
{
/*(* (*( (*(g_pppwcSymbol+1) )+2 ) + 3)) = L'K';*/
/*std::wcout<<g_pppwcSymbol[1][2][3];*/
***(g_pppwcSymbol + 1) = L'K';
std::wcout<<g_pppwcSymbol[1][0][0];
std::wcin.get();
return 0;
}
输出:K
下一步编辑:
工作模型:
static const int X = 2;
static const int Y = 3;
static const int Z = 4;
wchar_t g_pppwcSymbol[X][Y][Z];
int main(int argc, TCHAR **argv)
{
int iAccess = ((Y*Z) * 1) + ((Z) * 2) + 3;
*( (**g_pppwcSymbol) + iAccess) = L'K';
std::wcout<<g_pppwcSymbol[1][2][3];
std::wcin.get();
return 0;
}
输出:K
So, I've figured out how to dereference a multidimensional array like this:
#include <tchar.h>
#include <iostream>
wchar_t g_pppwcSymbol[2][3][4];
int main(int argc, TCHAR **argv)
{
(* (*( (*(g_pppwcSymbol+1) )+2 )+3 ) ) = L'K';
std::wcout<<g_pppwcSymbol[1][2][3];
std::wcin.get();
return 0;
}
Output: K
However, I've also heard that the compiler transforms this into a 1-dimensional array, is that correct? This would mean that all the wchar_t elements follow up each other in the memory. So there must be a way to do something like this:
***(g_pppwcSymbol + x) = value;
However I'm not sure how this works exactly. Could someone elaborate?
EDIT:
this seems to work so far:
int main(int argc, TCHAR **argv)
{
/*(* (*( (*(g_pppwcSymbol+1) )+2 ) + 3)) = L'K';*/
/*std::wcout<<g_pppwcSymbol[1][2][3];*/
***(g_pppwcSymbol + 1) = L'K';
std::wcout<<g_pppwcSymbol[1][0][0];
std::wcin.get();
return 0;
}
output: K
NEXT EDIT:
Working model:
static const int X = 2;
static const int Y = 3;
static const int Z = 4;
wchar_t g_pppwcSymbol[X][Y][Z];
int main(int argc, TCHAR **argv)
{
int iAccess = ((Y*Z) * 1) + ((Z) * 2) + 3;
*( (**g_pppwcSymbol) + iAccess) = L'K';
std::wcout<<g_pppwcSymbol[1][2][3];
std::wcin.get();
return 0;
}
Output: K
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编译器实际上将
[]
运算符转换为那些数学计算,因此您将遇到困难......但这是一个有趣的练习,所以我会一起玩。g_pppwcSymbol[2][3][4
] 实际上是一个由 2 个元素组成的大型数组,由 3 个元素组成的大型数组,由 4 个元素组成的大型数组。因此,应该为您提供最后一个数组的最后一个数组的最后一项的位置。只要每个元素是 1 字节大。
该数组只是一个大小为“
element_count
*element_size
”的内存区域,[]
运算符将数组位置作为指针并添加 < code>element_size *index
它可能包含其他数组,因为它可以像其他内存结构一样计算它们的大小,我相信您会找到大量有关的文档 。这在互联网上都有详细介绍。
The compiler actually transforms the
[]
operator into just those mathematical calculations, so you are going about this the hard way... But it's an interesting exercise so I'll play along.g_pppwcSymbol[2][3][4
] is actually a 2 elements large array of 3 elements large array of 4 elements large. Thus,should give you the position of the last item of the last array of the last array. As long as each element is 1 byte large.
The array simply is a memory area the size of "
element_count
*element_size
and the[]
operator takes the array location as a pointer and addselement_size
*index
. It may contain other arrays since it can compute their sizes the same way as other memory structures.BTW, I'm sure you'll find plenty of documentation about this in details all over the internet.