Dienstag, 29. September 2015

结构体--构造一个新的数据类型

struct student //结构体类型的定义
{
      int id;
      char name[20];
      char sex;
      int age;
      float score;
      char addr[30];
};

注意:student是数据类型,不是变量(等同于int float,char这些数据类型)是用来定义其他变量的。它属于组合数据类型;


如何定义结构体变量:

            1 直接用已声明的结构体类型定义变量名
             student        studen1,      student2;
           ( 结构体类型名)  (结构体变量名)

 对比:int a;(student 相当与 int)

            2在声明类型的同时定义变量
             struct student 
              {
                 int id;
                 char name[20];
                 char sex;
                 int age;
                 float score;
                 char addr[30];
             }yp1,yp2;



第一种:只有结构体定义
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. struct stuff{  
  2.         char job[20];  
  3.         int age;  
  4.         float height;  
  5. };  



第二种:附加该结构体类型的“结构体变量”的初始化的结构体定义
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //直接带变量名Huqinwei  
  2. struct stuff{  
  3.         char job[20];  
  4.         int age;  
  5.         float height;  
  6. }Huqinwei;  


也许初期看不习惯容易困惑,其实这就相当于:
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. struct stuff{  
  2.         char job[20];  
  3.         int age;  
  4.         float height;  
  5. };  
  6. struct stuff Huqinwei;  






Keine Kommentare:

Kommentar veröffentlichen