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

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

问题描述

我修改了一些code和跨我无法理解的声明来了:

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] [用户] 可以正常工作,当它被声明为 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罗伯特建议。然而,没有人知道如何在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。真的是无非3-元件1d阵列更哪个元素类型是另一种四元件1d阵列元素类型是为int *

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]

现在code是所有 userMask [2] [maskElement] [用户]

要做到这一步一步凭有效C code(如果你不明白一切不用担心但在以下):

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!! */

现在,如果将我们发生什么粒子阵列[1] 品脱[1] ?首先将推进粒子阵列的sizeof(INT [3])字节( 3 * sizeof的(INT))中,第二个将仅的sizeof(INT *)字节前进。所以实际上当第一给你正确的阵列阵列[0] [1] ,第二次给你(的char *)数组[0] +的sizeof(INT *),这是什么地方我们真的不希望它是。但抓错了偏移不是一切。因为它不知道一个数组被访问时,它会尝试跨preT什么是品脱[1] 为int * 。说你的阵列是用 0×00 初始化。然后,它会做下一个索引步骤中,基于关地址为0x00(否则品脱[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天全站免登陆