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 ++:指针算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!