第四章 指標 Pointer(進階),第四章指標pointer

來源:互聯網
上載者:User

第四章 指標 Pointer(進階),第四章指標pointer

第四章

 指標 pointer(進階)


希望初學者在入門的時候,可以看一些英文的原著,我感覺英文書籍是原意,而一些現在中文的翻譯是加上了譯者的一些理解,多少是拿來的東西,所以一些東西我希望能夠按照自己的來。

 

&A  就是取存放A的位置,我們可以將這個位置複製給pointer variable。

*A  從A所指的位置中“提取數值”

 

接上回,首先取個例子:

Algorithm findBigger(x, y)

Pre: x, y :: refToInteger are valid references

Post: no change to data

Return: the reference to the larger of *x, *y

refToInteger temp

if (*x ≥ *y)

temp ← x

else

temp ← y

end if

return temp

值得注意的是,在這裡的x和y是指兩個address,我們可以從這兩串數字中讀取到它所指的數值。當給一個pointer temp賦值時,我們輸入這個pointer的值就是x的address。

 

在C++的執行個體中就是如此:

int *findBigger(int *x, int *y){

int *temp;

if (*x >= *y)

temp = x;

else

temp = y;

return temp;

}

這時候,temp所存放的就是地址。

 

 

Algorithm swap(a, b)

Pre: a :: refToInteger

b :: refToInteger

a, b contain valid references

Post: the contents of *a and *b

are exchanged

Integer temp ← *a

*a ← *b

*b ← temp


下回,我將會講記憶體和指標的運用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.