小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

(轉)例解GNU C之零長數(shù)組與變長數(shù)組

 WUCANADA 2012-02-21

例解GNU C之零長數(shù)組與變長數(shù)組

分類: 漫談C語言 60人閱讀 評論(0) 收藏 舉報

    前言:計算機語言是編譯器和程序員交流的依據(jù)和規(guī)范,GNU C是GCC特有的功能,在Linux內(nèi)核中被廣泛應用。

    幫助文檔:http://gcc./onlinedocs/gcc-4.6.2/gcc/C-Extensions.html#C-Extensions

 

    1、零長數(shù)組

    GNU C允許聲明長度為零的數(shù)組,但它只能被用于結構體的最后一個成員。

    舉例,如清單1: 

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. struct line {  
  5.     int length;  
  6.     char contents[0];  
  7. };  
  8.   
  9. int main(void)  
  10. {  
  11.     int i, count = 9;  
  12.     char letter = 'A';  
  13.   
  14.     struct line *thisline = (struct line *)malloc(sizeof(struct line) + count);  
  15.       
  16.     thisline->length = count;  
  17.     for (i = 0; i < count; i++)  
  18.     thisline->contents[i] = letter++;  
  19.       
  20.     printf("sizeof(struct line) = %d\n"sizeof(struct line));  
  21.       
  22.     for (i = 0; i < thisline->length; i++)  
  23.     printf("%c ", thisline->contents[i]);  
  24.     printf("\n");  
  25.       
  26.     return 0;  
  27. }  

    例子輸出結果: 

  1. sizeof(struct line) = 4  
  2. A B C D E F G H I   

    如例子中的第6行,contents就是一個零長數(shù)組,在sizeof看來它所占的空間為零。

    在ISO C99中,使用變長數(shù)組也可以實現(xiàn)同樣的功能,如清單2: 

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. struct line {  
  5.     int length;  
  6.     char contents[];  
  7. };  
  8.   
  9. struct line thisline = { 5, {'1''2''3''4''5' } };  
  10.   
  11. int main(void)  
  12. {  
  13.     int i;  
  14.       
  15.     printf("sizeof(struct line) = %d\n"sizeof(struct line));  
  16.     printf("sizeof(thisline) = %d\n"sizeof(thisline));  
  17.       
  18.     for (i = 0; i < thisline.length; i++)  
  19.     printf("%c ", thisline.contents[i]);  
  20.     printf("\n");  
  21.       
  22.     return 0;  
  23. }  

    例子輸出結果: 

  1. sizeof(struct line) = 4  
  2. sizeof(thisline) = 4  
  3. 1 2 3 4 5   

    變長數(shù)組是不完全數(shù)據(jù)類型,不能使用sizeof獲得它的大小。

    注意,此結構體的變量必須在函數(shù)外定義和初始化,否則會報錯: 

  1. error: non-static initialization of a flexible array member  
  2. error: (near initialization for 'thisline')  

    不能使用這樣的形式: 

  1. struct mystruct {  
  2.     int arr[];  
  3. };  

    否則會報錯: 

  1. error: flexible array member in otherwise empty struct  

    2、變長數(shù)組

    在支持變長數(shù)組之前,C語言數(shù)組的大小是在聲明時確定的(下標是一個常量表達式)并一直保持不變。所謂變長數(shù)組就是指數(shù)組的大小可以在運行時指定,如清單3: 

  1. #include <stdio.h>  
  2.   
  3. int main(void)  
  4. {  
  5.     int i;  
  6.   
  7.     scanf("%d", &i);  
  8.   
  9.     int arr[i];  
  10.   
  11.     printf("sizeof(arr[%d]) = %d\n", i, sizeof(arr));  
  12.   
  13.     return 0;  
  14. }  

    例子輸出結果: 

  1. sizeof(arr[6]) = 24 //輸入數(shù)字6  
  2.   
  3. sizeof(arr[9]) = 36 //輸入數(shù)字9  

    輸入不同的值,數(shù)組的大小隨之改變。

    變長數(shù)組作為參數(shù)進行傳遞的例子,如清單4: 

  1. #include <stdio.h>  
  2.   
  3. int sum(int num, int arr[num])  
  4. {  
  5.     int i, total = 0;  
  6.   
  7.     for (i = 0; i < num; i++)  
  8.     total += arr[i];  
  9.   
  10.     return total;  
  11. }  
  12.   
  13. int main(void)  
  14. {  
  15.     int a[] = {1, 2, 3, 4};  
  16.     int b[] = {5, 6, 7, 8, 9, 10};  
  17.   
  18.     printf("a[] total value: %d\n", sum(sizeof(a)/sizeof(a[0]), a));  
  19.     printf("b[] total value: %d\n", sum(sizeof(b)/sizeof(b[0]), b));  
  20.   
  21.     return 0;  
  22. }  

    例子輸出結果: 

  1. a[] total value: 10  
  2. b[] total value: 45  

    函數(shù)sum形參中的arr可以匹配任意的一維整型數(shù)組。

    注意,num一定要聲明在變長數(shù)組arr之前

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多