typedef使用大全)

來源:互聯網
上載者:User

typedef使用大全(轉)

一、數組
typedef到處都是,但是能夠真正懂得typedef使用的不算太多。對於初學者而言,看別人的源碼時對到處充斥的typedef往往不知所錯,而參考書又很少,所以在此給出一個源碼,供大家參考

#include <stdio.h>
#include <iostream.h>

/* 避免Visual C的for與標準for的不同 */
#define for if (0); else for

/* dim(a)是用於計算a的維數,不過只能計算數組的維數,不能計算指標的維數 */
#define dim(a) (sizeof(a)/sizeof(a[0]))

/* N1到N4是幾個常量,以枚舉的形式定義 */
enum {N1 = 2, N2 = 3, N3 = 4, N4 = 5};

/* 這個C程式員都知道,就是將DataType定義為int型,便於擴充 */
typedef int DataType;

/* 定義一個一維數組,數組的元素維整型值 */
typedef DataType ARR1[N4];

/* 再定義一個一維數組,數組的元素維ARR1型,不過ARR1又是一個數組,所以
* ARR2 實際上是一個矩陣
*/
typedef ARR1 ARR2[N3]; /* 此處完全等價為typedef int ARR2[N3][N4];*/

/* 按照ARR2的解釋,ARR3也是一個一維數組,不過數組元素的類型是ARR2的類型
* 所有ARR3是一個三維數組
*/
typedef ARR2 ARR3[N2]; /* 此處完全等價為typedef int ARR3[N2][N3][N4];*/

/* 分別用定義好的ARR1,ARR2,ARR3定義三個變數a, b, c */
ARR1 a; /* 此處完全等價於:int a[N4]; */
ARR2 b; /* 此處完全等價於:int b[N3][N4]; */
ARR3 c; /* 此處完全等價於:int c[N2][N3][N4]; */

/* 下面函數給大家個樣本看a,b,c如何使用 */
void exam_1()
{
    for (int i=0; i<dim(a); i++) a[i] = i+1;

    for (int i=0; i<dim(b); i++) for (int j=0; j<dim(b[0]); j++)
        b[i][j] = (i+1)*10 + (j+1);

    for (int i=0; i<dim(c); i++) for (int j=0; j<dim(c[0]); j++)
        for (int k=0; k<dim(c[0][0]); k++) c[i][j][k] = (i+1)*100 + (j+1)*10 + (k+1);

    printf("/nThe a is :/n");
    for (int i=0; i<dim(a); i++) printf("%4d ", a[i]);
    printf("/n");

    printf("/nThe b is :/n");
    for (int i=0; i<dim(b); i++)
    {
        for (int j=0; j<dim(b[0]); j++) printf("%4d ", b[i][j]);
        printf("/n");
    }

    printf("/nthe c is:/n");
    for (int i=0; i<dim(c); i++)
    {
        for (int j=0; j<dim(c[0]); j++)
        {
            for (int k=0; k<dim(c[0][0]); k++) printf("%4d ", c[i][j][k]);
            printf("/n");
        }
        printf("/n");
    }
}

/* 下面函數給大家示範數組在記憶體中的排列 */
void exam_2()
{
    int *pn = NULL;

    pn = (int /*等價於 pn = &a[0]; */ *)a;
    printf("/nThe a is :/n");
    for (int i=0; i<sizeof(a)/sizeof(DataType); i++) printf("%4d ", pn[i]);
    printf("/n");

    pn = (int /*等價於 pn = &b[0][0]; */ *)b;
    printf("/nThe b is :/n");
    for (int i=0; i<sizeof(b)/sizeof(DataType); i++) printf("%4d ", pn[i]);
    printf("/n");

    pn = (int /*等價於 pn = &c[0][0][0]; */ *)c;
    printf("/nThe c is :/n");
    for (int i=0; i<sizeof(c)/sizeof(DataType); i++) printf("%4d ", pn[i]);
    printf("/n");
}

int main(int argc, char* argv[])
{
    exam_1();
    exam_2();
    return 0;
}
二、結構體續 數組

#define S(s) printf("%s/n", #s); s

typedef struct _TS1{
    int x, y;
} TS1, *PTS1, ***PPPTS1; // TS1是結構體的名稱,PTS1是結構體指標的名稱
// 也就是將結構體struct _TS1 命名為TS1,
// 將struct _TS1 * 命名為 PTS1
// 將struct _TS1 *** 命名為 PPPTS1

typedef struct { // struct後面的結構體說明也可以去掉
    int x, y;
} TS2, *PTS2;

typedef PTS1 *PPTS1; // 定義PPTS1是指向PTS1的指標

typedef struct _TTS1{
    typedef struct ITTS1 {
        int x, y;
    } iner;
    iner i;
    int x, y;
} TTS1;

//結構體內部的結構體也一樣可以定義
typedef TTS1::ITTS1 ITS1;

void test_struct()
{
    // 基本結構體重定義的使用
    TS1 ts1 = {100, 200};
    PTS1 pts1 = &ts1; // 完全等價於TS1* pts1 = &ts1;
    PPTS1 ppts1 = &pts1; // 完全等價於TS1** ppts1 = &pts1;
    PPPTS1 pppts1 = &ppts1; // 完全等價於 TS1*** pppts1 = &ppts1;

    TS2 ts2 = {99, 88};
    PTS2 pts2 = &ts2;   // 完全等價於 TS2* pts2 = &ts2;

    TTS1 itts1 = {{110, 220}, 10, 20};
    Its1* rits1 = &itts1.i;
    ITS1* &its1 = rits1; // 等價於 TTS1::ITTS1 *its1 = &(itts1.i);

    printf("ts1/t = (%d, %d)/n*pts1/t = (%d, %d)/n"
           "**ppts1/t = (%d, %d)/n***pppts1= (%d, %d)/n/n",
            ts1.x, ts1.y, pts1->x, pts1->y,
            (**ppts1).x, (**ppts1).y, (***pppts1).x, (***pppts1).y);
    printf("ts2/t = (%d, %d)/n*pts2/t = (%d, %d)/n/n",
        ts2.x, ts2.y, pts2->x, pts2->y);
    printf("itts1/t = [(%d, %d), %d, %d]/n*its1/t = (%d, %d)/n/n",
        itts1.i.x, itts1.i.y, itts1.x, itts1.y, its1->x, its1->y);

    S(pts1->x = 119);
    S(pts2->y = 911);
    S(its1->x = 999);

    printf("ts1/t = (%d, %d)/n*pts1/t = (%d, %d)/n"
           "**ppts1/t = (%d, %d)/n***pppts1= (%d, %d)/n/n",
            ts1.x, ts1.y, pts1->x, pts1->y,
            (**ppts1).x, (**ppts1).y, (***pppts1).x, (***pppts1).y);
    printf("ts2/t = (%d, %d)/n*pts2/t = (%d, %d)/n/n",
        ts2.x, ts2.y, pts2->x, pts2->y);
    printf("itts1/t = [(%d, %d), %d, %d]/n*its1/t = (%d, %d)/n/n",
        itts1.i.x, itts1.i.y, itts1.x, itts1.y, its1->x, its1->y);

    S((*ppts1)->y = -9999);
    printf("ts1/t = (%d, %d)/n**ppts1/t = (%d, %d)/n/n",
        ts1.x, ts1.y, (*ppts1)->x, (*ppts1)->y);

    S((**pppts1)->x = -12345);
    S((***pppts1).y = -67890);
    printf("ts1/t = (%d, %d)/n*pts1/t = (%d, %d)/n"
           "**ppts1/t = (%d, %d)/n***pppts1= (%d, %d)/n/n",
            ts1.x, ts1.y, pts1->x, pts1->y,
            (**ppts1).x, (**ppts1).y, (***pppts1).x, (***pppts1).y);
}

三、指向函數的指標在typedef的使用中,最麻煩的是指向函數的指標,如果沒有下面的函數,你知道下面這個運算式的定義以及如何使用它嗎?
int (*s_calc_func(char op))(int, int);

如果不知道,請看下面的程式,裡面有比較詳細的說明

// 定義四個函數
int add(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);
// 定義指向這類函數的指標
typedef int (*FP_CALC)(int, int);

// 我先不介紹,大家能看懂下一行的內容嗎?
int (*s_calc_func(char op))(int, int);

// 下一行的內容與上一行完全相同,
// 定義一個函數calc_func,它根據操作字元 op 返回指向相應的計算函數的指標
FP_CALC calc_func(char op);

// 根據 op 返回相應的計算結果值
int calc(int a, int b, char op);

int add(int a, int b)
{
    return a + b;
}
int sub(int a, int b)
{
    return a - b;
}
int mul(int a, int b)
{
    return a * b;
}
int div(int a, int b)
{
    return b? a/b : -1;
}
// 這個函數的用途與下一個函數作業和調用方式的完全相同,
// 參數為op,而不是最後的兩個整形
int (*s_calc_func(char op)) (int, int)
{
    return calc_func(op);
}

FP_CALC calc_func(char op)
{
    switch (op)
    {
    case '+': return add;
    case '-': return sub;
    case '*': return mul;
    case '/': return div;
    default:
        return NULL;
    }
    return NULL;
}

int calc(int a, int b, char op)
{
    FP_CALC fp = calc_func(op); // 下面是類似的直接定義指向函數指標變數
       // 下面這行是不用typedef,來實現指向函數的指標的例子,麻煩!
        int (*s_fp)(int, int) = s_calc_func(op);
        // ASSERT(fp == s_fp); // 可以斷言這倆是相等的
    if (fp) return fp(a, b);
    else return -1;
}

void test_fun()
{
    int a = 100, b = 20;
    printf("calc(%d, %d, %c) = %d/n", a, b, '+', calc(a, b, '+'));
    printf("calc(%d, %d, %c) = %d/n", a, b, '-', calc(a, b, '-'));
    printf("calc(%d, %d, %c) = %d/n", a, b, '*', calc(a, b, '*'));
    printf("calc(%d, %d, %c) = %d/n", a, b, '/', calc(a, b, '/'));
}

運行結果
   calc(100, 20, +) = 120
   calc(100, 20, -) = 80
   calc(100, 20, *) = 2000
   calc(100, 20, /) = 5

 

聯繫我們

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