ダークホースプログラマー_C言語構造体

3891 ワード

iOS、Android、Javaトレーニング、お客様とのコミュニケーションを楽しみにしています
実際の応用では、あるオブジェクトは異なるタイプのデータから構成され、例えば学生の名前は文字列型であり、学号は整形または文字列型であり、性別は列挙型であり、身長は浮動小数点型である.これらのデータを配列で格納することは不可能であることは明らかである.C言語は、このオブジェクトを構造データ型である構造体で表す.
1構造体変数の定義
構造体は列挙と同様に、構造体を定義してから、構造体変数を定義する必要があります.
1.1構造体の定義
structは構造体のキーワードであり、構造体の一般的な形式を定義します.
/*
struct     {
    1    1;
    2    3;
......
    n    n;
};
*/

struct Student {
    char *name; //   
    int age; //   
    float height; //   
};

1.2構造体変数の定義
構造体を定義したら、構造体変数を定義できます.一般的な3種類の構造体変数の定義方法:
(1)構造体タイプを定義してから変数を定義する
(2)構造体タイプを定義しながら変数を定義する
(3)構造体タイプ変数を直接定義し、タイプ名を省略する
/*

struct Student {
    char *name; //   
    int age; //   
    float height; //   
};
struct Student stu;  //     

//     
struct Student {
    char *name; //   
    int age; //   
    float height; //   
} stu;

//     
struct {
    char *name; //   
    int age; //   
    float height; //   
} stu;

2構造体を定義する上での注意点
(1)構造体内部のデータ型は、その構造体そのものではなく、すなわち再帰的に定義できない
(2)構造体内部のデータ型は、定義済みの構造体であってもよい
(3)構造体タイプを定義し,システムがintタイプ自体に空間を割り当てないように,このタイプの構成を説明しただけで,記憶空間を割り当てていない.構造体タイプに属する変数を定義する場合にのみ、システムはその変数にストレージスペースを割り当てます.
(4)構造体変数が占有するメモリ領域は、そのメンバーが占有するメモリの和であり、各メンバーはメモリに定義された順序で順番に並べられる
/*
struct Student {
    char *name; //   
    int age; //   
    float height; //   
    Student stu;  //                      
};  */   

struct Date {
    int year;
    int month;
    int day;
};

struct Student {
    char *name; //   
    int age; //   
    float height; //   
    Date birthday;  //                      
};     //           

struct Student stu;  //   stu      

struct {
    char *name; //   
    int age; //   
    float height; //   
} stu;    //stu       ,4 + 4 + 4 = 12

3構造体変数の初期化
構造体変数を初期化するには、次の2つの方法があります.
(1)各メンバの初期値を1対の括弧{}に順番に入れ,カンマで区切って1対1に対応付けする.
(2)括弧内で、ポイントとメンバー名を組み合わせて値を付け、メンバー名の順序は任意である.
なお、初期化付与は、変数を定義しながらのみ行うことができ、初期化付与と変数の定義を分けることはできません.
 
struct Student {
    char *name; //   
    int age; //   
    float height; //   
};   

struct Student stu = {"zhangsan",12,165.7f};  //        
struct Student stu1 = {.age = 12,.name = "zhangsan"};  //        

/*      
struct Student stu;
stu = {"zhangsan",12,165.7f};
*/

4構造体変数のメンバーへのアクセス
(1)一般的に構造体変数に対する操作はメンバー単位で行うが、参照の一般形式は、構造体変数名である.メンバー名.
(2)あるメンバーが構造体変数である場合、メンバー演算子「.」を連続的に使用することができる.最下位レベルのメンバーにアクセスします.
(3)同じタイプの構造体変数間で全体的な付与が可能である.
struct Date {
    int year;
    int month;
    int day;
};

struct Student {
    char *name; //   
    int age; //   
    float height; //   
    Date birthday;  
};     

struct Student stu;  //   stu      

stu.age = 17;  //       
stu.birthday.year = 2000;  //           

struct Student stu1 = stu;  // stu     stu1

5構造体配列
(1)構造体配列変数の定義は,構造体変数の定義と同様に3つの方式がある.初期化も構造体変数と同じです.
(2)構造体配列のアクセス方式は配列と同じである.
struct Date {
    int year;
    int month;
    int day;
} date[3];  //         

struct Student {
    char *name; //   
    int age; //   
    float height; //   
};     
struct Student stu[2] = {{"zhangsan",14,123.6f},{.name = "lisi",.age = 12}};  //         ,    

struct {
    char *name; //   
    int age; //   
    float height; //   
} stus[3];   //          
stus[0].age = 12;  //          

6構造体へのポインタ
(1)構造体変数はいずれも独自の記憶空間とアドレスを持っているので,ポインタは構造体変数を指すこともできる.
(2)構造体ポインタ変数の定義形式:struct構造体名*ポインタ変数名.
(3)構造体ポインタが構造体メンバーにアクセスする方法:
1.(*ポインタ変数名).メンバー名
2.ポインタ変数名->メンバー名
struct Date {
    int year;
    int month;
    int day;
} ;  //         

struct Date date = {2001,12,12};

struct Date *p = &date;

(*p).year = 1990;   //     ,      
p->month = 11;   //     ,      


iOS、Android、Javaトレーニング、お客様とのコミュニケーションを楽しみにしています