關於數組和指標:二維數組和指向數組的指標

來源:互聯網
上載者:User

#include <stdio.h><br />void main(){<br />int a[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};<br />int *p,(*pt)[4],i,j;<br />p=a[0];<br />pt=a;<br />for(j=0,i=0;i<4;i++,j++){<br />printf("p is %d,and it's address is,pt is %d/n",*p++,*((*pt)+j));<br />}<br />printf("p's address is %d,pt's address is %d/n",p,pt);<br />printf("a[0]'s address is %d,a's address is %d/n",a[0],a);<br />p=a[0];p++;<br />pt=a;pt++;<br />printf("when they add one ,the address are %d, and %d/n",p,pt);<br />//p++;<br />//pt++;</p><p>}

通過這段代碼,可以看出,對於二維數組,要想擷取其首地址(例子中的a,不是a[0])必須用指向數組的指標變數,如果p=a,則會報錯。

而a[0],代表的是第0行0列的地址(a代表0行的地址),這就可以區分,在地址加1時,a會加16,而a[0]只會加4。

註:

int型佔4個位元組在電腦記憶體中,一個位元組8位。

printf("%d,%d,%d",sizeof(int),sizeof(short),sizeof(long));

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.