• <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語言
            久久免费高清视频| 性欧美丰满熟妇XXXX性久久久| 精品久久久无码人妻中文字幕豆芽| 亚洲日本va中文字幕久久| 久久国产色AV免费观看| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 热久久国产欧美一区二区精品| 18禁黄久久久AAA片| 国产产无码乱码精品久久鸭| 精品久久久久久无码中文野结衣| 伊人久久大香线蕉综合热线| 97精品国产91久久久久久| 污污内射久久一区二区欧美日韩 | 夜夜亚洲天天久久| 亚洲精品97久久中文字幕无码| 97久久久久人妻精品专区| 午夜精品久久影院蜜桃| 青青青国产成人久久111网站| 东方aⅴ免费观看久久av| 久久精品成人免费观看97| 麻豆AV一区二区三区久久 | 精品综合久久久久久88小说| 久久久久亚洲av无码专区| 欧美亚洲国产精品久久高清| 亚洲天堂久久精品| 国产精品欧美久久久天天影视| 久久福利资源国产精品999| 精品一久久香蕉国产线看播放| 72种姿势欧美久久久久大黄蕉| 亚洲精品乱码久久久久久按摩 | 国産精品久久久久久久| 91精品国产高清91久久久久久| 精品久久久久久久国产潘金莲| 久久久久久国产精品无码下载| 久久美女人爽女人爽| 久久成人国产精品二三区| 东京热TOKYO综合久久精品| 亚洲va久久久噜噜噜久久| 久久人妻少妇嫩草AV蜜桃| 久久人人爽人人人人爽AV| 久久99热这里只有精品国产|