Linux C 程式(TEN)

來源:互聯網
上載者:User

標籤:

指標數組和二級指標

 1 #include<stdio.h> 2  3 int main(){ 4         int a[5] = {1,3,5,7,9}; 5         int *p[5], i; 6         int **pp = p; 7     //使p指標數組指向每一個a 8         for(i = 0 ; i < 5 ; i++){ 9                 p[i] =&a[i];10         }11     //【】優先順序高於* ,*(p[i]) p[i] = &a[i],so==> *p[i] = *&a[i] = a[i]12         for(i = 0 ; i < 5 ; i++){13                 printf("%d ",*p[i]);14         }15         printf("\n");16     //p 和 pp都是二級指標,i=0時,*pp=p[0] ,p[0]=&a[i],*pp=a[0].17     //執行pp++之後,pp指向p[1],*pp=p[1],p[1]=&a[1].18         for(i = 0 ; i < 5 ; i++,pp++){19                 printf("%d ",**pp);20         }21     22         return 0 ;23 }24 output:25 1 3 5 7 926 1 3 5 7 9


指標與數組的 關係

 1 nclude<stdio.h> 2  3 int main(){ 4         int a[2][5] = {1,3,5,7,9,2,4,6,8,10}; 5         //表示p是一個指標,指向含有五個元素的一維數組 6         int (*p)[5],i; 7         //p指向二維數組得第一行 8         p = a ; 9     //p=a 之後 , p=&a , *p = a[1].10     //p是一個數組指標11         for(i = 0 ; i < 5 ; i++){12                 printf("%d ",(*p)[i]);13         //(*p)[i] 是先取p的內容作為數組的起始地址,然後再去第i個元素。14         //*p[i]是先取以p為起始地址的第i個元素,該元素為指標,然後取該指標的內容。15         }16         printf("\n");17     //p移動到第二行18         p++;19         for(i = 0 ; i < 5 ; i++){20                 printf("%d ",(*p)[i]);21         }22         printf("\n");23 24         return 0;25 }26 27 int (*p)[5] ;//一個指標28 int  *p[5]  ; //含有五個元素的數組,長度為5,數組中得每一個元素指向一個整型變數。

 


函數和指標
1.指標作為函數得參數
作用是將一個變數的地址傳送到一個函數中

 1 #include<stdio.h> 2 void change(int i , int *p){ 3         i++; 4         if(p != NULL){ 5                 (*p)++;//根據地址找到變數b,b+1 6         } 7 } 8 int main(){ 9         int a = 5 , b = 10 ;10         change(a,&b);11         printf("a=%d,b=%d",a,b);12         return 0;13 }

 




2.返回指標得函數
 1 int *f(int i , int j); 
() 的優先順序高於 *
表示 f是一個函數,函數名前有一個指標,表示此函數得傳回值類型為指標。

 1 include<stdio.h> 2 //定義一個名稱為name得指標數組,每個數組元素都指向一個字串 3 char *name[7] = {"monday","tuesday","wednessday","thursday","friday","saturday","sunday"}; 4 //定義一個指標,這個指標指向一個字串 5 char *message = "wrong input"; 6 //返回指標的函數 7 char *week(int day){ 8         if(day < 0 || day > 7){ 9                 return message ;10         }else{11                 return name[day-1] ;12         }13 }14 15 16 int main(){17         int day ;18         char *p;19 20         printf("input a number of a week:\n");21         scanf("%d",&day);22     23         p = week(day);24         printf("%s\n",p);25 26         return 0;27 }

 



3.指向函數得指標 87頁----------

Linux C 程式(TEN)

聯繫我們

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