指针运算是否使用数组外? [英] Does pointer arithmetic have uses outside of arrays?

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

问题描述

我认为我理解指针运算的语义相当不错,但我只看到处理数组的例子。它有没有其他用途不能通过不透明的手段实现?我相信你可以找到一种方法与聪明的铸造使用它访问结构的成员,但我不知道为什么你会打扰。我最感兴趣的是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屋!

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