問題描述
我在 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++:指針算術的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!