C语言开发教程_结构体、结构体指针、tyepdef、union、enum



1. 结构体定义和初始化



//  :  一系列不同类型的数据的结合

// 数组存储的是一系列相同数据类型的集合

// 结构体是存储一系列不同数据类型的集合

struct Student  // 这时候并没有分配内存,只是声明了一个结构体变量,这个变量的名字是Student

{

    char name[20];

    int age;

    char gender[10];

    int classId;


};


struct Student2 

{

    char name[20];

    int age;

    char gender[10];

    int classId;


}Zeking;// 第二种定义的方式


// 第三种定义的方式

// 锁定结构体的变量的数量,这边分配了内存

struct {

    char name[20];

    int age;

    char gender[10];

    int classId;


}stud3, stu4, stu5; // stud3 全局的匿名结构体的变量名,类似java中的匿名内部类,为了锁定结构体的变量的数量


struct People{

    char name[20];

    int age;

}Lucy = {"Lucy",90};


// 强调:类型! = 变量。 结构体名代表的只是结构体类型,没有内存空间。

// 结构体中的成员可以单独使用

int main(){


    // 第一种 定义 的方式

    struct Student  stu1; // 这时候分配了内存,在栈里面,局部变量



    // =========================================================================


    struct People people1 = {"Zeking",20};

    struct People people2;

    // people2.name = "David";

    people2.age = 1;

    strcpy(people2.name, "lucy");

    people1.age = 10;


    printf("%s,%d \n", people1.name, people1.age);



    system("pause");

    return 0;

}

2. 结构体数组 结构体指针



struct People2{

    //char name[20];

    char *name;

    int age;

}Lucy2 = { "Lucy", 90 };


int main(){


    // 结构体数组 初始化

    int i;

    struct People2 stu[3] = { { "Zeking", 30 }, { "David", 32 }, { "Suci", 28 } };

    struct People2 s[5];

    for (i = 0; i < 5; i++){

        s[i].age = 20 + i;

        //strcpy(s[i].name,"Lucy");

        s[i].name = "lucy";

        // 如果是 是char name[20] 要用strcpy 赋值

        // 如果要 s[i].name = "lucy"  要把 name的 类型变为 char* name

        // 如果不明白可以去看上一篇博客


    }

    for (i = 0; i < 5; i++){

        printf("s %d:%s,%d\n",i,s[i].name,s[i].age);

    }


    // ============================ 结构体 指针 ============================


    struct People2 *p = stu;// 把数组的首地址赋值给p等价于  p = stu;


    struct People2 *p2;

    p2 = (People2 *)malloc(sizeof(struct People2) *4);

    // 定义了一个具有4个People2的变量的数组,然后将这个数组的地址赋给了p2

    printf("%#x \n",&p2);

    memset(p2, 0, sizeof(struct People2) * 4);// 初始化,都为0


    for (i = 0; i < 4; i++){

        //(p2 + i)->age = 20 + i;  // p2+i 涉及到指针的位移,位移的大小为sizeof(struct People2)*i

        //(p2 + i)->name = "zeking";

        // 也可以下面的写法

        p2[i].age = 20 + i;

        p2[i].name = "zeking";

    }

    for (i = 0; i < 4; i++){

        printf("p2 : %d:%s,%d\n", i, (p2 + i)->name, (p2 + i)->age);

    }



    system("pause");

    return 0;

}



3 . 结构体 里面添加函数指针



struct Man{

    int age;

    char *name;

    int(*Msg)(char *,int);

};


int message(char *str, int age){

    MessageBox(0, TEXT("hello"), TEXT("Lijian"), 0);

    return 0;

}


int main(){


    struct Man man;

    man.age = 40;

    man.name = "Zeking";

    man.Msg = message;


    man.Msg(man.name,man.age);


    system("pause");

    return 0;

}

4. 结构体中添加结构体指针成员变量



struct Node {

    int data;

    Node * next;

};


//ArrayList

 

    list;

//Node node;

//list.add(node);


// 在单链表的末尾添加一个数据

int enqueNode(Node *head, int data) {

    Node * node = (Node *)malloc(sizeof(Node));

    if (node == NULL) {

        return 0;

    }

    node->data = data;

    node->next = NULL;


    //不让head的 本身值 改变

    Node *p = head;

    while(p->next != NULL) {

        p = p->next;

    }


    p->next = node;

    /*  while (head->next != NULL) {

        head++;

    }*/

    return 1;

}


int main() {

    int num = 10;

    int i = 0;

    Node * list;

    list = (Node *)malloc(sizeof(struct Node));

    list->data = 0;

    list->next = NULL;


    for (i = 0; i < num; i++) {

        enqueNode(list, i+1);

    }


    while (list->next != NULL)

    {

        printf("%d \n", list->data);

        list = list->next;

    }

    system("pause");

    return 0;

}

  

5. typedef 指令



//就是别名

//java 代理

//并没有创建新的数据类型,只是给现有类型创建了别名


typedef int _in;

typedef char * string;


typedef int(*PFI)(char *, char *);


typedef Tnode * Treeptr;

typedef struct Tnode {

    char *word;

    int count;


    /*Tnode * left;

    Tnode * right;*/

    Treeptr left;

    Treeptr right;

} BinaryTreeNode;


int fun(char *, char *) {

    return 0;

}

int main() {

    _in a = 20;

    printf("%d\n", a);


    string str;

    str = "hello world";


    PFI fp;

    fp = fun;


    char * ch;

    ch = "hello world";


    BinaryTreeNode* node;

    node = (BinaryTreeNode *) malloc(sizeof(BinaryTreeNode));


    system("pause");

    return 0;

}

6. 公用体 ,枚举



//union

//将不同的数据类型的数据放到同一段内存里面。

//占用的内存大小是 数据类型里面占用最大的那个内存大小

union MyUnion

{

    int a;

    char b;

    float c;

};


int main() {

    MyUnion unio;


    unio.a = 10;

    unio.b = 'a';

    unio.c = 1.2f;

    printf("a: %#x, b: %#x, c: %#x\n", &unio.a, &unio.b, &unio.c);

    // 只能取到 最近赋值的变量

    printf("a: %d, b: %c, c: %f\n", unio.a, unio.b, unio.c);


    system("pause");

    return 0;

}


enum {

    monday = 10,

            saturday,  、、

    sunday,

};

猜你喜欢

转载自right-left.iteye.com/blog/2405640