C/C++:指针算术

C/C++: Pointer Arithmetic(C/C++:指针算术)
本文介绍了C/C++:指针算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Pointer Arithmetic 中读了一点,我发现了两件事我不明白,也不知道它的用途

I was reading a bit in Pointer Arithmetic, and I came upon 2 things I couldn't understand neither know it's use

address_expression - address_expression

还有

address_expression > address_expression

谁能给我解释一下,它们是如何工作的以及何时使用.

Can someone please explain them to me, how do they work and when they are used.

我想说的是如果我只取两个地址并减去它们会产生什么

What I meant to say is what do they produce if I just take two addresses and subtract them

如果我取两个地址并比较它们是什么结果或基于比较

And If I take two addresses and compare them what is the result or comparing based upon

减去地址的结果我现在明白了,但是比较地址还是不明白.

I now understand the result of subtracting addresses, but comparing addresses I still don't get it.

我知道 1<2,但是一个地址如何大于另一个地址以及它们的比较对象是什么

I understand that 1<2, but how is an address greater than another one and what are they compared upon

推荐答案

指针减法产生相同类型的两个指针之间的数组元素数.

Pointer subtraction yields the number of array elements between two pointers of the same type.

例如

int buf[10] = /* initializer here */;

&buf[10] - &buf[0];  // yields 10, the difference is 10 elements

指针比较.例如,对于 > 关系运算符:如果左侧的指向数组元素或结构成员,则 > 操作产生 1在右侧的指向数组元素或结构成员之后,否则产生 0.记住数组和结构是有序序列.

Pointer comparison. For example, for the > relational operator: the > operation yields 1 if the pointed array element or structure member on the left hand side is after the pointed array element or structure member on the right hand side and it yields 0 otherwise. Remember arrays and structures are ordered sequences.

 &buf[10] > &buf[0];  // 1, &buf[10] element is after &buf[0] element

这篇关于C/C++:指针算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)