为什么压扁在C多维数组非法的? [英] Why is flattening a multidimensional array in C illegal?

查看:102
本文介绍了为什么压扁在C多维数组非法的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的书(指针基于C由Kenneth臭佬)说,下面的非法的,虽然它工作正常。

My book (Pointers on C by Kenneth Reek) says that the following is illegal although it works fine.

  int arr[5][5];
  int *p=&arr[2][2];
  p=p+3; // As array is stored in row major form I think this 
         //should make p point to arr[3][0]

书上说留下一行到下一行是非法的。但我不明白为什么。

The book says leaving one row to the next row is illegal. But I cannot understand why.

推荐答案

我凝胶在这一段时间,我会尽我所能来解释,我的认为的他来自何处,但不读书书,这将是在最佳-猜想。

I gelled on this for awhile, and I'll try my best to explain where I think he's coming from, though without reading the book, it will be at-best-conjecture.

首先,从技术上说,你提出(或他提议)增量并不违法;的解引用的它。该标准允许您提前一个指向一个过去从它被采购估值阵列序列的最后一个元素,而不是取消引用。将其更改为 P = + 4 两个的是非法的。

First, technically, the increment you propose (or he proposed) isn't illegal; dereferencing it is. The standard allows you to advance a pointer to one-past the last element of the array sequence from which it is being sourced for valuation, but not for dereference. Change it to p = p + 4 and both are illegal.

除此之外,数组的线性足迹不顶住, AR [2] 有一个类型,它是 INT [5] 。如果你不相信,请考虑以下,这一切都是正确输入:

That aside, the linear footprint of the array not withstanding, ar[2] has a type, and it is int[5]. If you don't believe that, consider the following, all of which is correctly typed:

int ar[5][5];
int (*sub)[5] = ar+2;   // sub points to 3rd row
int *col = *sub + 2;    // col points to 3rd column of third row.
int *p = col + 3;       // p points to 5th colum of third row.

这是否落到 AR [3] [0] 是不相关的你超过参与指针数学维度的声明幅度。其结果不能合法解除引用,并分别比3偏移较大,也不可能得到法律甚至评估。

Whether this lands on ar[3][0] isn't relevant You're exceeding the declared magnitude of the dimension participating in the pointer-math. The result cannot legally be dereferenced, and were it larger than a 3-offset, nor could it be even legally evaluated.

记住,正在处理数组是 AR [2] ;不仅仅是 AR ,并说,同样是被宣布为大小= 5。它是挟着靠在相同之流其他两个阵列是不相关的寻址的目前的被完成的。我相信克里斯托夫的回答以建议为重复的问题应该是一个选择了彻底的解决方案。尤其是提到的 C99§6.5.6,P8 这虽然罗嗦,会出现如下搭配:

Remember, the array being addressed is ar[2]; not just ar, and said-same is declared to be size=5. That it is buttressed up against two other arrays of the same ilk isn't relevant to the addressing currently being done. I believe Christoph's answer to the question proposed as a duplicate should have been the one selected for outright-solution. In particular, the reference to C99 §6.5.6, p8 which, though wordy, appears below with:

当有整型的前pression加上或减去
  从一个指针,其结果具有指针操作数的类型。如果
  操作数的指针指向数组对象的元素,数组
  是足够大的,结果指​​向一个元件从偏移
  原始的元素以使得的下标之差
  结果和原始的数组元素等于整数前pression。
  换言之,如果前pression P指向到的第i个元素
  数组对象,前pressions(P)+ N(等同于N +(P))和(P)-N
  (这里N的值n)指向,分别第i + n个和
  I-n个数组对象的元素,只要它们存在。此外,如果
  恩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.

很抱歉的垃圾邮件,但粗体的亮点是什么,我相信是有关​​你的问题。通过为你解决,的你要离开阵列正在处理的,因此走进UB。总之,它的工作原理(一般),但就是不合法的。

Sorry for the spam, but the bolded highlights are what I believe is relevant to your question. By addressing as you are, you're leaving the array being addressed, and as such walking into UB. in short, it works (usually), but is isn't legal.

这篇关于为什么压扁在C多维数组非法的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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