是否指针运算有阵列以外的用途? [英] Does pointer arithmetic have uses outside of arrays?
问题描述
我想我明白指针运算的语义还算不错,但我只看到过的例子使用数组的时候。它有不能由较少不透明的装置来实现的任何其他用途?我敢肯定,你可以找到聪明铸造的方式来使用它访问一个结构的成员,但我不知道为什么你会烦。我用C最感兴趣,但我会用C ++标记,因为答案很可能适用也有。
I think I understand the semantics of pointer arithmetic fairly well, but I only ever see examples when dealing with arrays. Does it have any other uses that can't be achieved by less opaque means? I'm sure you could find a way with clever casting to use it to access members of a struct, but I'm not sure why you'd bother. I'm mostly interested in C, but I'll tag with C++ because the answer probably applies there too.
编辑,根据回答接收到目前为止:我知道指针可以在许多非阵列的环境中使用。我特别想知道的指针运算,例如递增,走差异,等等。
Edit, based on answers received so far: I know pointers can be used in many non-array contexts. I'm specifically wondering about arithmetic on pointers, e.g. incrementing, taking a difference, etc.
推荐答案
如果你遵循语言标准的信,然后指针运算的只有的指向数组时定义,而不是在任何另一种情况。
If you follow the language standard to the letter, then pointer arithmetic is only defined when pointing to an array, and not in any other case.
一个指针可能指向数组中的任何元素,或者一步到位的过去的数组的结尾。
A pointer may point to any element of an array, or one step past the end of the array.
这篇关于是否指针运算有阵列以外的用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!