在實際工作中,很多情況我們需要將不同型別的資料組織起來一起應用,比如:學校在期末考試結束後,通常都要進行學生成績的填報和查詢工作。一個一年級小學生的資訊通常包括:姓名、學號、性別、年齡、語文成績、數學成績等。姓名、性別是一個字元型別的資料,而年齡、學號為整數型別,語文成績、數學成績通常都為單精度浮點型別資料。我們知道不同型別的資料是不能放到同一個陣列裡面的,那麼在C語言中是否有方法將這些不同資料類別的資料組織到一起呢?回答是:“有,可以用結構來處理這種問題。”,接下來,就讓我們來了解一下,什麼叫結構。“結構”是一種構造型別,它是由若干“成員”組成的,每一個成員可以是一個基本資料型別或者又是一個構造型別。通常,一個結構的一般形式為:
struct 結構名
    {成員表列};
成員表列,由若干個成員組成,每個成員都是該結構的一個組成部分。對每個成員也必須作型別說明,其形式為:
型別說明符 成員名;
現在,讓我們一起來給學生來定義一個“結構”
struct student
{
    int num;  //學號
    char name[20];      //姓名
    char sex[2];    //性別
       int  age;        //年齡
    float chinesescore;  //語文成績
       float mathscore;     //數學成績
};
上面我們定義了一個名稱為“student”的結構,它包含了,學號、姓名、性別、年齡、語文成績和數學成績資訊。
那麼如何應用結構,在LoadRunner中應用結構的示例指令碼如下:
struct student
{
    int num;  //學號
    char name[8]; //姓名
       int  age;        //年齡
    char sex[2];    //性別
    float chinesescore;  //語文成績
       float mathscore;     //數學成績
};

 

Action()
{  //為結構陣列賦前2個結構陣列元素值
       struct student stu[3]={{101,”孫悟空“,30,”“,100.00,100.00},
                                             {102,”沙和尚“,28,”“,99.00,99.00},};
       struct student stu1={103,”白骨精“,99,”“};  //為結構變數stu1賦部分資料
       int i;

 

       stu1.chinesescore=90.50;      //stu1賦語文成績
       stu1.mathscore=89.00;         //stu1賦數學成績

 

       stu[2]=stu1;    //stu1變數賦給陣列元素stu[2]

 

       for (i=0;i<=2;i++) {
              lr_output_message(“—————————–“);
              lr_output_message(“%d個學生資訊:”,i+1);
              lr_output_message(“學號=%d”,stu[i].num);
              lr_output_message(“姓名=%s”,stu[i].name);
              lr_output_message(“性別=%s”,stu[i].sex);
              lr_output_message(“年齡=%d”,stu[i].age);
              lr_output_message(“語文成績=%.2f“,stu[i].chinesescore);
              lr_output_message(“數學成績=%.2f“,stu[i].mathscore);
              lr_output_message(“—————————–“);
       }

 

      
       return 0;
}
上面指令碼的輸出內容為:
Running Vuser…
Starting iteration 1.
Starting action Action.
Action.c(24): —————————–
Action.c(25): 1個學生資訊:
Action.c(26): 學號=101
Action.c(27): 姓名=孫悟空
Action.c(28): 性別=
Action.c(29): 年齡=30
Action.c(30): 語文成績=100.00
Action.c(31): 數學成績=100.00
Action.c(32): —————————–
Action.c(24): —————————–
Action.c(25): 2個學生資訊:
Action.c(26): 學號=102
Action.c(27): 姓名=沙和尚
Action.c(28): 性別=
Action.c(29): 年齡=28
Action.c(30): 語文成績=99.00
Action.c(31): 數學成績=99.00
Action.c(32): —————————–
Action.c(24): —————————–
Action.c(25): 3個學生資訊:
Action.c(26): 學號=103
Action.c(27): 姓名=白骨精
Action.c(28): 性別=
Action.c(29): 年齡=99
Action.c(30): 語文成績=90.50
Action.c(31): 數學成績=89.00
Action.c(32): —————————–
Ending action Action.
Ending iteration 1.
Ending Vuser…
當然,為了我們引用結構方便,您可以應用型別定義符“typedef”將“struct student”命名成簡潔的、明瞭的名稱。C語言允許由使用者自己定義型別說明符,即:型別定義符“typedef”,允許由使用者為資料型別取“別名”。上面的結構指令碼,我們可以用“typedef”實現同樣的功能。
typedef struct student
{
    int num;  //學號
    char name[8]; //姓名
       int  age;        //年齡
    char sex[2];    //性別
    float chinesescore;  //語文成績
       float mathscore;     //數學成績
} STU;

 

Action()
{     //為結構陣列賦前2個結構陣列元素值
       STU stu[3]={{101,”孫悟空“,30,”“,100.00,100.00},
                                             {102,”沙和尚“,28,”“,99.00,99.00},};
       STU stu1={103,”白骨精“,99,”“};//為結構變數stu1賦部分資料
       int i;

 

       stu1.chinesescore=90.50;      //stu1賦語文成績
       stu1.mathscore=89.00;         //stu1賦數學成績

 

       stu[2]=stu1;    //stu1變數賦給陣列元素stu[2]

 

       for (i=0;i<=2;i++) {
              lr_output_message(“—————————–“);
              lr_output_message(“%d個學生資訊:”,i+1);
              lr_output_message(“學號=%d”,stu[i].num);
              lr_output_message(“姓名=%s”,stu[i].name);
              lr_output_message(“性別=%s”,stu[i].sex);
              lr_output_message(“年齡=%d”,stu[i].age);
              lr_output_message(“語文成績=%.2f“,stu[i].chinesescore);
              lr_output_message(“數學成績=%.2f“,stu[i].mathscore);
              lr_output_message(“—————————–“);
       }

 

      
       return 0;
}
請大家注意黑體字部分,應用“typedef”後,您會發現在定義結構變數的時候,我們省略了“struct student”而用自定義的符號“STU”來宣告相應變數即可,非常方便。還有一點,不知道您注意到沒有,就是我們在定義的時候書寫了這樣的語句“STU stu[3]”,如果您在Delphi等語言中書寫,它會提示您書寫錯誤的,原因是這些語言是不區分大小寫的,而在C語言中是區分大小寫的,“STU”和“stu”分別代表兩個不同的內容。