C/C++: Pointer Arithmetic(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




Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(将 RGB 转换为 HSV 并将 HSV 转换为 RGB 的算法,范围为 0-255)
How to convert an enum type variable to a string?(如何将枚举类型变量转换为字符串?)
When to use inline function and when not to use it?(什么时候使用内联函数,什么时候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意义;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相关吗?)