IOS 階段學習第八天筆記(指標),ios第八天

來源:互聯網
上載者:User

IOS 階段學習第八天筆記(指標),ios第八天

                                                        IOS學習(C語言)知識點整理

一、指標

1)概念:儲存變數的地址的一個變數。

2) 資料存放區類型分析

   1、text (程式碼片段) :儲存二進位的可執行代碼

   2、data(初始化的資料區段) 儲存初始化的全域變數以及初始化的static修飾的變數,分為唯讀資料區段

     (如:字串常量,整型常量)及可讀可寫資料區段,靜態儲存。

   3、data(未初始化的資料區段)儲存未初始化的全域變數及未初始化的static修飾的變數,靜態儲存。

   4、heap(堆區) 記憶體使用量需要手動申請,手動釋放,空間非常大,通常儲存大容量的資料,執行效率比較低

       使用比較麻煩,動態儲存裝置。

 5、stack(棧區)記憶體空間比較小,函數調用局部變數開闢都是棧上,使用效率非常高。

3)取地址符 “&”,間接定址符 “ * ”,“%p” 列印地址的格式字元

4)指標標示符:* 例如: int *p   表示定義了一個指標p,*是指標標示,int表示p指向的類型(裡面儲存的是int型變數的地址)

5)各種類型的指標,在記憶體中擁有的空間都是一樣的,64位系統下面都是8個位元組(32位是4個位元組)。 

6)指標運算 指標對象 ++,表示指向地址向後位移,位移量為sizeof(指向的類型);- -則表示向前位移。

7)野指標:未初始化值的指標,裡面是個隨機地址,可以任意修改該地址對應的值,會給系統造成不可控制的影響。

8)null 指標 p=NULL=0; 0是非法地址,0地址不可賦值,不可讀寫。

9)指標的優點

   1、為函數提供修改調用變數的靈活手段

   2、可以使函數有多個傳回值

   3、可以改善某些子程式的效率 ,在傳遞資料時,如果資料區塊較大(比如資料緩衝區域或比較大的結構),這時就可使用指標傳遞地址而

        不是實際資料,即可提 高傳輸速度,又節省大量記憶體。

  4、為動態資料結構(如 二叉樹、鏈表)提供支援。

10)void* 指標,通配指標,可是指向任意類型。

11)指標的應用,函數中指標作為函數的入參。

12)int *a;  指標變數,指向int型資料。

13)指標數組  即指向一個數群組類型的指標   

       例如: int *p[4] : 表示4個指向int的指標.每個指標都是獨立的,沒有關係; p[i] 是一個 int 型指標

       p[i]=*(p+i)

 執行個體代碼:

 1 int add(int *a[],int len1,int len2) 2 { 3     int sum = 0; 4     for(int i=0;i<len1;i++){ 5         //a[i]第i個指標=第i個一維數組 6         for(int j=0;j<len2;j++){ 7             sum = sum + *(a[i]+j); 8         } 9     }10     return sum;11 }12 13 int main()14 {16     int a[4][3]={{1,2,3},{4,5,6}, {7,8,9},{10,11,12}};17     int *p[4];//指標數組,沒初始化不能用,野指標組18     for(int i=0;i<4;i++){19         p[i]=a[i];//a[i]表示第i行的一個數組20     }22    int sum = add(p,4,3);23    printf("sum = %d\n",sum);24    return 0;25 }

14)數組指標 

執行個體代碼:

 1 int add(int(*p)[3],int len) 2 { 3     int sum = 0; 4     for(int i=0;i<len;i++){ 5         //p+i 表示指向第i個數組 *(p+i)=a[i] 6         for(int j=0;j<3;j++){ 7         //a[i][j] = *(a[i]+j) = *(*(p+i)+j) 8             sum = sum + *(*(p+i)+j); 9         }10     }11 12     return sum;13 }14 15 int main()16 {17     int a[4][3]={{1,2,3},{4,5,6}, {7,8,9},{10,11,12}};19     //a[0],a[1],a[2],a[3]:4個數組都有3個元素21     int(*p)[3];//可以指向一個有3個元素的數組23     //p = &(a[0]);//&a[0] = &(*(a+0))25     p = a;//由上面的那個式子化簡27     int sum =add(a, 4);//既然p=a,就可以這麼寫29     printf("sum %d \n",sum);31  }

15)函數指標  int (*pfunc)(int,int); 定義了一個函數指標,必須要指向 int XXX(int a,int b) 這種樣式的函數

 執行個體代碼:

 1 int add(int a,int b) 2 { 3       return a+b; 4 } 5  6 int main(){ 7     int a = 10; 8     int b = 20; 9     int sum;10     int (*pfunc)(int,int);11 12     pfunc = add;//函數名相當於一個指向其函數入口指標常量14     sum = pfunc(a,b);16     printf("sum = %d\n",sum);18     return 0;20 }

16) 使用指標 實現函數回調

 實現代碼:

 1  void print_hello() 2 { 3     printf("hello world\n"); 4 } 5  6  void print_date() 7 { 8     time_t rawtime;10     struct tm * timeinfo;12     time ( &rawtime );14     //擷取系統本地時間 需要引用 time標頭檔 ,#include <time.h>16     timeinfo = localtime ( &rawtime );18      printf ( "%4d-%02d-%02d %02d:%02d:%02d\n",1900+(*timeinfo).tm_year, 1+(*timeinfo).tm_mon,20             (*timeinfo).tm_mday,(*timeinfo).tm_hour,(*timeinfo).tm_min,(*timeinfo).tm_sec);22 }23 24 // void(*pfunc)()25 void print_all(void(*pfunc)(),int count)26 {27     for(int i=0;i<count;i++){28         pfunc();29     }31 }33  34 int main()35 {37     print_all(print_date,4);//迴圈輸出4次39     print_all(print_hello,1);41     return 0;42 }

17)使用指標實現遞迴演算法

 實現代碼:

void sort_array(int *a,int len){   //冒泡排序   int temp;   for(int i=0;i<len-1;i++){         for(int j=0;j<len-1-i;j++){         if(*(a+j)>*(a+j+1))            {               temp = *(a+j);               *(a+j) = *(a+j+1);              *(a+j+1) = temp;             }         }     }}
int main(){ int a[5]={1,20,30,4,5}; int *p; p = a; //傳遞數組:2種形式 數組或者指標 print_array(a, 5); return 0;}

 

18)二級指標 指向指標的指標。

 執行個體代碼:

 1 void swap(int **p,int **q) 2 { 4     int temp; 6     temp = **p; 8     **p = **q;10     **q = temp;12 }13  15 int main()16 {18     int a = 10;20     int b = 20;22     int *p = &a;//p是一個指標,指向int型24     int **q;//q是一個指標,指向 int *型,int* 型就是指向int的指標類型,p就是這樣一個類型26     q = &p;//*q=*(&p)=p28     printf("%d\n",**q);//*q=p,**q=*p=a30     int *p_b = &b;32     int **pp_b = &p_b;34     swap(q,pp_b);36     printf("a=%d,b=%d\n",a,b);38     return 0;39 }

19)const 修飾符,唯讀  表現形式: const 資料類型 變數名 ,const 直接修飾的變數不可以改變,但加了*的變數可修改。

 執行個體代碼:

 1 int main() 2 { 4     int value1 = 10; 6     int value2 = 20; 8     const int a = 100;10     printf("a=%d\n",a);12     a = 20;//const修飾的變數不能修改14     const int *p = &value1;16     p = &value2;18     //*p = 100;//*p不可以修改,p可以改20     int const *m = &value1;22     m = &value2;24     //*m = 10;//*m不可以修改,m可以改26     int *const n;//n不可以改,*n可以改28     const int *const q//q不可以改,*q不可以改30     const int *const m,*n;//m,*m不可以改,n可以改32     *n =100;//*n也不可以改34     const int *const m, *const n;//m,*m,n不可以改36     *n = 100;//*n也不可以改38     int *const m, *const n;//m,n不可以改,*m,*n可以改,40     *n = 100;41 }

 

相關文章

聯繫我們

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