Accesing使用单个指针二维数组 [英] Accesing a 2D array using a single pointer

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

问题描述

有万吨code的像这样的:

There are tons of code like this one:

#include <stdio.h>

int main(void)
{
    int a[2][2] = {{0, 1}, {2, -1}};
    int *p = &a[0][0];

    while (*p != -1) {
        printf("%d\n", *p);
        p++;
    }
    return 0;
}

不过,在此基础上回答,行为是不确定的。

N1570。 6.5.6 P8:

N1570. 6.5.6 p8:

当有整型的前pression加上或减去
  从一个指针,其结果具有指针操作数的类型。如果
  操作数的指针指向数组对象的元素,数组
  是足够大的,结果指​​向一个元件从偏移
  原始的元素以使得的下标之差
  结果和原始的数组元素等于整数前pression。
  换言之,如果前pression P指向到的第i个元素
  数组对象,前pressions(P)+ N(等同于N +(P))和(P)-N
  (这里N的值n)指向,分别第i + n个和
  的i-n个阵列对象的元素,只要它们的存在。此外,
  如果EX pression p指向数组对象的最后一个元素时,
  前pression(P)+1点中的一个过去的数组对象的最后一个元素,
  如果前pression Q点中的一个过去的数组的最后一个元素
  对象,前pression(Q)-1点到所述阵列的最后一个元素
  目的。如果指针操作数和结果指向元素都
  相同的数组对象,或者一个过去阵列的最后一个元素的
  对象,该评估也不得产生溢出;否则,
  行为是不确定的。如果结果点中的一个过去的最后一个元素
  数组对象的,不得被用来作为一元的操作数
  *这是评估经营者。

When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i−n-th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

有人能详细解释一下吗?

Can someone explain this in detail?

推荐答案

该阵列谁的基址(指针指向第一个元素) P 被分配的类型为 INT [2] 。这意味着在地址 P 可以合法的取消引用仅在位置 * P *(p + 1),或者如果你preFER标符号, p [0] p [1] 。此外, P + 2 保证是一个合法的评估的作为一个地址,而媲美的到其他地址顺序,但可不可以被取消引用。这是一个过去的地址。

The array who's base address (pointer to first element) p is assigned is of type int[2]. This means the address in p can legally be dereferenced only at locations *p and *(p+1), or if you prefer subscript notation, p[0] and p[1]. Furthermore, p+2 is guaranteed to be a legally evaluated as an address, and comparable to other addresses in that sequence, but can not be dereferenced. This is the one-past address.

您发布的code违反提领 P 一旦它通过其所驻留在数组中的最后一个元素的一个过去的规则。在它被驻留在挟着对抗类似尺寸的另一个阵列的方向是不相关的引正式定义

The code you posted violates the one-past rule by dereferencing p once it passes the last element in the array in which it is homed. That the array in which it is homed is buttressed up against another array of similar dimension is not relevant to the formal definition cited.

这表示,实践的它的工作原理,但往往是说。的观察的行为是不是,而且不应该被考虑的定义的行为的。正因为它的工作原理并没有作出正确选择。

That said, in practice it works, but as is often said. observed behavior is not, and should never be considered, defined behavior. Just because it works doesn't make it right.

这篇关于Accesing使用单个指针二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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