[整理]函數的聲明中的數組參數

來源:互聯網
上載者:User

標籤:style   class   blog   code   color   strong   

在函數的聲明的數組參數中,並為指明數組的長度。因為在C中,允許單個函數操縱任意長度的一維數組。這個特性不利的一面是函數沒法知道該數組的長度。如果確實需要知道數組的長度,必須作為一個單獨的參數傳遞給函數。

當數組名作為實參時,傳遞給函數的實際上是一個指向數組起始位置的指標。

/*開發環境:vs2010 window xp sp3*/#include <stdio.h>int main(void){       void array_len(int v[]);    int v[]= { 70, 30, 40, 60, 10, 20, 50, 100, 80, 90 };    int len;    array_len(v);    len=sizeof(v)/sizeof(int);//40/4=10, sizeof(v)返回的是一維數組的元素總共所佔的自己數10*4    printf("%d\n",len);    //10        return 0;}void array_len(int v[]){    int len=sizeof(v)/sizeof(int);//4/4=1,sizeof(v)返回的是int *p 指標的長度,為4個位元組    printf("%d\n",len);    //1}

以上int改成long也是一樣的結果。

需要注意的的是:
指標和long int 佔據的是一樣的,代表了這個平台的字寬,即機器的最大定址寬度,32位就是4位元組。

聯繫我們

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