`int *userMask[3][4]` 指向什么? [英] what is `int *userMask[3][4]` pointing to?

查看:14
本文介绍了`int *userMask[3][4]` 指向什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改一些代码并遇到一个我无法理解的声明:

I am modifying some code and came across a declaration that I am having trouble understanding:

int *userMask[3][4] = {0};

这到底是指什么?它是一个矩阵,其中每个元素都是一个指针吗?还是指向大小为 [3][4] 的矩阵?

What exactly is this pointing to? Is it a matrix where every element is a pointer? Or is it pointing to a matrix of size [3][4]?

谢谢

我想我的问题是 userMask[2][maskElement][user] 在声明为 int 时如何工作.userMask 不是必须是 int[] 才能正常工作吗?我一定没有正确理解这一点...

I guess my question is how userMask[2][maskElement][user] can work when it is declared as int. Wouldn't userMask have to be int[] for that to work properly? I must not be understanding this right...

顺便说一句,感谢您对 cdecl Robert 的建议.但是,有人知道如何在 XP 命令提示符下使用它吗?我能得到的只是语法错误:(

On a side note, thanks for your suggestion about cdecl Robert. However, does anyone know how to use it in an XP command prompt? All I can get is syntax error :(

推荐答案

简短回答

给定的 userMask 声明为

Short answer

Given userMask is declared as

int *userMask[3][4];

然后 userMask 的类型为 int*[3][4].它是一个指向 int 指针的二维数组.外维的大小为3,内维的大小为4.其实就是一个元素类型为int*<的4元素一维数组,元素类型为3元素的一维数组/代码>.

then userMask has type int*[3][4]. It's a 2d array of pointers to int. The size of the outer dimension is 3, the size of the inner dimension is 4. Really that is nothing more than a 3-element 1d array which element type is another 4-element 1d array which element type is int*.

所以如果你这样做

userMask[2][maskElement][user]

然后基本上使用前两个索引从二维数组中选择特定的指针:

then essentially with the first two indices you pick the particular pointer out of the 2d array:

int * p = userMask[2][maskElement];

然后您通过执行从该指针偏移的某处选择一个 int

then you pick an int somewhere offset from that pointer by doing

p[user]

现在所有代码都在 userMask[2][maskElement][user] 中.

now that code is all in userMask[2][maskElement][user].

使用有效的 c 代码逐步完成(如果您还不了解以下所有内容,请不要担心):

To do it step by step with valid c code (don't worry if you don't understand everything yet in the following):

int * userMask[3][4] = { { 0 } };
int ** pa = userMask[2]; /* int*[4] becomes int** implicitly */
int * pi = pa[maskElement];
int i = pi[user];

assert(i == userMask[2][maskElement][user]);

数组和指针的区别

所以我想我向你展示了一些重要的东西.上面的数组包含指向数组的指针.让我们看看它们的行为有何不同,这是许多 C 程序员不希望的:

Difference between Arrays and Pointers

So i think i show you something important. The array above does not contain pointers to arrays. Lets look how different they behave, which many c programmers don't expect:

int array[5][4][3];
/* int[4][3] implicitly converts to int(*)[3] (pointer to first element) */
int (*parray)[3] = array[0]; 
int ** pint = (int**) array[0]; /* wrong!! */

现在,如果我们执行 parray[1]pint[1] 会发生什么?第一个将 parray 前进 sizeof(int[3]) 个字节(3 * sizeof(int)),第二个将仅前进 sizeof( int*) 字节.所以实际上虽然第一个给你正确的数组 array[0][1],第二个给你 ( char * )array[0] + sizeof( int* )>,这是我们并不真正想要的地方.但是抓住错误的偏移量并不是全部.因为它不知道数组被访问,它会尝试将 pint[1] 处的内容解释为 int*.假设你的数组是用 0x00 初始化的.然后它将根据地址 0x00 执行下一个索引步骤(例如,执行 pint[1][0]).哦不 - 完全未定义的行为!因此,强调差异非常重要.

Now, what will happen if we do parray[1] and pint[1] ? The first will advance parray by sizeof(int[3]) bytes (3 * sizeof(int)), the second will advance by only sizeof( int* ) bytes. So actually while the first gives you the correct array array[0][1], the second will give you ( char * )array[0] + sizeof( int* ), which is somewhere we don't really want it to be. But grabbing the wrong offset is not all about it. Because it doesn't know an array is accessed, it will try to interpret what is at pint[1] as an int*. Say your array was initialized with 0x00. Then it will do the next index step based off address 0x00 (Doing pint[1][0] for example). Oh noes - utterly undefined behavior! So it's really important to stress the difference.

这超出了您的要求,但我认为了解这些细节非常重要.特别是如果您想将二维数组传递给函数,那么这些知识非常有用.

This was more than you asked for, but I think it's quite important to know these details. Especially if you want to pass 2d arrays to functions then this knowledge is really useful.

这篇关于`int *userMask[3][4]` 指向什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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