• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            劉加加在成長(C++語言學習中)

            學習、工作、生活,這是三個問題。

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              38 隨筆 :: 0 文章 :: 24 評論 :: 0 Trackbacks

            傳遞數組參數,其核心就是傳遞該數組的首地址。然后函數再通過數組、指針等,用該數組的首地址構造一個新的數組或指針,再通過傳遞過來的數組大小,對該數組進行操作。

            #include <stdio.h>
            #define SIZE 4
            int sumbyarr(int a[],int n);
            int sumbypointer(int *p,int n);
            int sumbyaddress(int address,int n);
            int sumbypointer2(int *begin,int *end)
            int main(void)
            {
                
            int arr[SIZE]={10,20,30,40};
                
            int sum1;
                
            //使用數組作為形參接收數組 
                sum1=sumbyarr(arr,SIZE);
                printf(
            "the total of the arr by array is:%d\n",sum1);
                
            //使用指針作為形參接收數組 
                int sum2;
                sum2
            =sumbypointer(arr,SIZE);
                printf(
            "the total of the arr by pointer is:%d\n",sum2);
                
            //使用地址作為形參接收數組 
                int sum3;
                sum3
            =sumbyaddress(arr,SIZE);
                printf(
            "the total of the arr by address is:%d\n",sum3);
                   
                //使用兩個指針形參接收
                int sum4;
                sum4=sumbypointer2(arr,arr+SIZE);
                printf("the total of the arr by pointer2 is:%d\n",sum4);


                getchar();
                
            return 0;

            int sumbyarr(int a[],int n)

                
            int index;
                
            int total=0;
                
            for(index=0;index<n;index++)
                {
                    total
            +=a[index];
                }
                
            return total;
            }
            int sumbypointer(int *p,int n)
            {
                
            int index;
                
            int total=0;
                
            for(index=0;index<n;index++,p++)
                {
                    total
            +=*p;
                }
                
            return total;
            }
            int sumbyaddress(int address,int n)
            {
                
            int *p=address;
                
            int index;
                
            int total=0;
                
            for(index=0;index<n;index++,p++)
                {
                    total
            +=*p;
                }
                
            return total;
            }
            int sumbypointer2(int *begin,int *end)
            {
                int total=0;
                while(begin<end)
                {
                    total+=*begin;
                    begin++;
                }
                return total;
            }

            以上代碼演示怎樣定義有數組作為參數的函數。
            如果數組作為參數傳遞進入某個函數,并且在這個函數中改變了數組元素的值,那么程序返回后,這個數組元素的值有沒有改變呢?

            當然,值改變了,因為數組是通過地址傳遞的,改變了被調函數中數組的值也就意味著同時改變了主調函數中數組的值。因為它們的地址是相同的。
            posted on 2008-05-16 23:54 劉加加 閱讀(1383) 評論(0)  編輯 收藏 引用 所屬分類: 學習C語言
            丁香五月网久久综合| 99久久无色码中文字幕人妻| 久久精品www人人爽人人| 久久99精品久久久久久久久久 | 久久精品视频网| 久久精品亚洲欧美日韩久久| 亚洲日本va午夜中文字幕久久 | 国产农村妇女毛片精品久久| 久久天天躁狠狠躁夜夜不卡| 亚洲人成网亚洲欧洲无码久久| 99久久er这里只有精品18| 午夜精品久久久久久影视777| 日韩精品久久无码中文字幕| 99久久婷婷国产一区二区| 国产精品久久久久蜜芽| 999久久久免费国产精品播放| 99精品国产99久久久久久97| 国内精品伊人久久久久影院对白| 亚洲中文字幕久久精品无码APP | 国内精品久久久久久久97牛牛| 久久精品国产亚洲Aⅴ香蕉| 伊人久久大香线蕉亚洲| 国产日韩久久免费影院| 九九久久99综合一区二区| 亚洲乱码精品久久久久..| 亚洲欧洲中文日韩久久AV乱码| 99久久精品这里只有精品| 精品综合久久久久久888蜜芽| 久久综合久久综合亚洲| 国産精品久久久久久久| 久久久免费精品re6| 国产精品99久久久精品无码| 蜜桃麻豆www久久国产精品| 色综合久久久久| 伊人久久精品线影院| 久久91亚洲人成电影网站| 97久久精品国产精品青草| 乱亲女H秽乱长久久久| 无码久久精品国产亚洲Av影片 | 亚洲人成网站999久久久综合| 久久九九久精品国产免费直播|