指向数组转换的指针 [英] Pointer to array conversion

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

问题描述

8.3.4/8 N3797:

8.3.4/8 N3797:

[示例:

考虑int x[3][5];

这里的 x 是一个 3 × 5 的整数数组.当 x 出现在表达式中时,它被转换为指向(三个中的第一个)五元整数数组.在表达式中x[i] 等价于*(x+i)x首先转换为指针如所述;然后将x+i转换为x的类型,这涉及到将 i 乘以指针指向的对象的长度,即五个整数对象 [...]

Here x is a 3 × 5 array of integers. When x appears in an expression, it is converted to a pointer to (the first of three) five-membered arrays of integers. In the expression x[i] which is equivalent to *(x+i), x is first converted to a pointer as described; then x+i is converted to the type of x, which involves multiplying i by the length of the object to which the pointer points, namely five integer objects [...]

由于 x 的类型是5 个整数的 3 个数组的数组",我们也有 x+i.假设 i = 2;

Since the type of x is an "array of 3 arrays of 5 integers" we have that x+i too. Assume that i = 2;

x + i(称他为arr)元素在转换为5个整数的3个数组后的值是多少?我的意思是 arr[3] 等于什么?

What is the value of the x + i (Call him arr) elements after convertion to array of 3 arrays of 5 integers? I mean what arr[3] equals to?

推荐答案

arr[3] 是一个包含五个整数的数组.原因如下:

arr[3] is an array of five integers. The reason is the following:

当 x 出现在表达式中时,它被转换为指向(三个中的第一个)五元整数数组

When x appears in an expression, it is converted to a pointer to (the first of three) five-membered arrays of integers

这意味着在表达式中使用 arr 将导致类型 int(*)[5](衰减).

this means any use of arr in an expression would result in having type int(*)[5] (decayed).

对于 x[3],它等价于 *(x+i),首先将 x 翻译成 int(*)[5] 输入并推进它,然后你取消引用(记住 *)那个指针,从而得到 int[5] 类型.

For x[3], which is equivalent to *(x+i), you first get x translated into a int(*)[5] type and advance it, then you dereference (remember the *) that pointer and thus get type int[5].

不幸的是,指针指向一个无效的内存位置,因此对这个由五个整数组成的数组的任何操作都是未定义的行为/访问冲突.

Unfortunately the pointer pointed to an invalid memory spot and therefore any operation on this array of five integers is undefined behavior / access violation.

我同意这段话确实不清楚,这两个句子几乎似乎是一个在另一个之前的(也有点晦涩).

I agree that the passage really isn't clear about this, it almost seems the two sentences are one before the other (kinda obscure too).

我会改写为:

这里 x 是一个 3 × 5 的整数数组.当 x 出现在表达式中时,它被转换为指向(三个中的第一个)五成员的指针整数数组.在表达式 x[i] 中,它等价于*(x+i), x 首先被转换为一个指针,如上所述;在指针增量 x+i 之后(以字节方式涉及将 i 乘以长度指针指向的对象,即五个整数对象),x+1 被转换为x所指向的类型[...]

Here x is a 3 × 5 array of integers. When x appears in an expression, it is converted to a pointer to (the first of three) five-membered arrays of integers. In the expression x[i] which is equivalent to *(x+i), x is first converted to a pointer as described; after the pointer increment x+i (which in a byte-wise fashion involves multiplying i by the length of the object to which the pointer points, namely five integer objects), x+1 gets converted to the type pointed by x [...]

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

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