电竞比分网-中国电竞赛事及体育赛事平台

分享

C語言typeof詳解

 海漩渦 2015-03-17

轉(zhuǎn)自:http://blog./uid-28458801-id-4200573.html
C語言typeof詳解 2014-04-08 17:00:49

分類: C/C++

操作系統(tǒng):ubuntu10.04

前言:
    typeof關(guān)鍵字是C語言中的一個新擴展,這個特性在linux內(nèi)核中應(yīng)用非常廣泛。

一,說明
    typeof的參數(shù)可以是兩種形式:表達式類型。

    1,表達式的的例子:
        typeof(x[0](1)
        這里假設(shè)x是一個函數(shù)指針數(shù)組,這樣就可以得到這個函數(shù)返回值的類型了。
        如果將typeof用于表達式,則該表達式不會執(zhí)行。只會得到該表達式的類型。
        以下示例聲明了int類型的var變量,因為表達式foo()是int類型的。由于表達式不會被執(zhí)行,所以不會調(diào)用foo函數(shù)。
            extern int foo();
            typeof(foo()) var;

    2,參數(shù)的例子:
        typeof(int *) a,b;
            等價于:
            int *a,*b;


二,實例
    1,把y定義成x指向的數(shù)據(jù)類型:
           typeof(*x) y;
    2,把y定義成x指向數(shù)據(jù)類型的數(shù)組:
           typeof(*x) y[4];
    3,把y定義成一個字符指針數(shù)組:
            typeof(typeof(char *)[4] y;
    這與下面的定義等價:
            char *y[4];

    4,typeof(int *) p1,p2; /* Declares two int pointers p1, p2 */
            int *p1, *p2;

    5,typeof(int) *p3,p4;/* Declares int pointer p3 and int p4 */
            int *p3, p4;

    6,typeof(int [10]) a1, a2;/* Declares two arrays of integers */
            int a1[10], a2[10];


,局限
    typeof構(gòu)造中的類型名不能包含存儲類說明符,如externstatic。不過允許包含類型限定符,如constvolatile。
    例如,下列代碼是無效的,因為它在typeof構(gòu)造中聲明了extern:
        typeof(extern int) a;



四,文件參考
    1,http://blog.csdn.net/wslong/article/details/7728811
    2,http://gcc./onlinedocs/gcc/Typeof.html#Typeof

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多