美文网首页
ndk04_结构体,typedef,公用体,枚举

ndk04_结构体,typedef,公用体,枚举

作者: IT魔幻师 | 来源:发表于2017-09-01 16:37 被阅读68次

    一、结构体定义和初始化

    #include <stdlib.h>
    #include "string.h"
    
    //什么是结构体:一系列不同类型的数据的结合。
    //强调:类型!=变量。 结构体名代表的只是结构体类型,没有内存空间。
    //结构体中的成员可以单独使用。
    
    //1.定义一个全局结构体
    struct Student{
        char name[20];
        int age;
    }Lucy = {"lucy",20};  //Lucy 结构体的全局类型变量  
    
    
    //2.定义一个全局匿名的结构体
    struct {
        char name[20];
        int age;
        int classId;
    }stu3;  //stu3 全局匿名结构体的变量名
    
    
    //3.锁定结构体的变量的数量
    struct {
        char name[20];
        int age;
        int classId;
    }stu4,stu5; 
    
    
    int main(){
        //初始化方式1
        struct Student stul = {"lucy",20};
        
        //初始化方式2
        struct Student stu2;
        strcpy(stu2.name,"lucy");
        stul.age = 20;
    
        //初始化方式3 见第一个定义处
    
        printf("%s,%d \n",stu1.name,stu1.age);
    
        system("pause");
        return 0;
    }
    

    二、结构体数组

    //1.定义一个全局结构体
    struct Student{
        char *name;
        int age;
    }Lucy ;  //Lucy 结构体的全局类型变量  
    
    
    int main(){
        int i;
        //1.定义结构体数组并初始化(使用大括号将每个结构体括起来)
        struct Student stu[3] = { { "lucy", 30 }, { "lilei", 32 }, { "Hanmeimei", 35 } };
    
        //2.定义结构体数组并单个初始化
        struct Student s[5];
        for (i = 0; i < 5; i++){
            s[i].age = 20 + i;
            //strcpy(s[i].name, "lucy");
            s[i].name = "lucy";
        }
    
        for (i = 0; i < 5;i++){
            printf("s %d:%s,%d\n", i, s[i].name, s[i].age);
        
        }
    
        system("pause");
    
        return 0;
    }
    

    三、结构体指针

    • 1.使用方式一:
        struct Student{
            char *name;
            int age;
        }Lucy ; 
        
        
        int main(){
            //定义结构体指针
            struct Student stu[3] = { { "lucy", 30 }, { "lilei", 32 }, { "Hanmeimei", 35 } };
            struct Student *stu = stu;
        
            //以上定义等价于
            //struct Student *stud;
            //stud = stu;
        
            system("pause");
            return 0;
        }
    
    • 2.使用方式二:
        #include "stdafx.h"
        #include <stdlib.h>
        #include "string.h"
        
        //1.定义一个全局结构体
        struct Student{
            char *name;
            int age;
        }Lucy ;  //Lucy 结构体的全局类型变量  
        
        
        int main(){
            int i;
            int a;
            //定义结构体指针
            struct Student *stud;
            stud = (Student *)malloc(sizeof(struct Student) * 4);//4个元素的结构体数组
            printf("%#x\n", stud); //地址
        
            //初始化
            //memset: 将这一片内存 以stud为首地址的连续大小为 sizeof(struct Student) * 4 的内存 全部赋值为0。
            memset(stud, 0, sizeof(struct Student) * 4); 
            
            //赋值:
            for ( i = 0; i < 4; i++){
                //用法一:
                //(stud+i)->age = 20 + i;  //首地址向后位移
                //(stud + i)->name = "lucy";
        
                //用法二:
                stud[i].age = 20 + i;
                stud[i].name = "lucy";
            }
        
            //打印
            for ( a = 0; a < 4; a++){
                printf("stud %d:%s,%d\n", a, (stud + a)->name, (stud + a)->age);
            }
            system("pause");
            return 0;
        }
    
    • 3.结构体中添加函数指针成员变量
        #include <stdlib.h>
        #include "string.h"
        #include <Windows.h>
        
        struct Man{
            int age;
            char *name;
            int(*Msg)(char *, int);
        };
        
        int message(char * str, int age){
            //弹出一个对话框 hello
            MessageBox(0, TEXT("hello"), TEXT("hubin"), 0);
            return 0;
        }
        int main(){
        
            struct Man man;
            man.age = 40;
            man.name = "胡斌";
            man.Msg = message;
            man.Msg(man.name, man.age);
        
            return 0;
        }
    
    • 4.结构体中添加结构体指针成员变量(java中的list集合原理:单列表数据结构)
        #include <stdlib.h>
        #include "string.h"
        
        //单链表数据结构
        struct Node{
            int data;
            Node *next; //指向下一个指针
        };
        
        //在单列表的末尾添加一个数据
        int enqueNode(Node *head, int data){
            Node *node = (Node*)malloc(sizeof(Node));
            if (node == NULL){ //如果没有分配到空间,
                return 0;
            }
        
            node->data = data;
            node->next = NULL;
        
            Node *p = head;
            while (p->next != NULL){
                p = p->next;
            }
            p->next = node;
        
            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;
        }
    

    四、typedef 指令(起别名)

    typedef并没有创建新的数据类型,只是给现有类型创建了别名
    • 1.定义别名

        #include <stdlib.h>
        
        //将int 取了一个别名叫做 _in
        typedef int _in;
        //给char* 定义个别名叫string
        typedef char*  string;
        //给函数指针定义 别名
        typedef int(*PFI)(char *, char *);
        
        int fun(char *, char*){
            return 0;
        }
        
        int main(){
            _in a = 20;
            printf("%d\n", a);
        
            string str;
            str = "HelloWord";
        
            char*ch;
            ch = "HelloWord";
        
            PFI fp;
            fp = fun;
        
            system("pause");
            return 0;
        }
      
    • 2.二叉树数据结构(有了面向对象的性质)

        #include "stdafx.h"
        #include <stdlib.h>
        
        typedef Tnode* Treeptr;
        //二叉树的数据结构 给Tnode取别名
        typedef struct Tnode{
            char *word;
            int count;
        
            //Tnode * left;
            //Tnode * right;
        
            //上面的代码 用别名代替
            Treeptr  left;
            Treeptr  right;
        
        }BinaryTreeNode;
        
        
        int main(){
            BinaryTreeNode * node;
            node = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
        
            system("pause");
            return 0;
        }
    

    五、公用体

        #include "stdafx.h"
        #include <stdlib.h>
        
        //好处:用一个内存单元来存储多种类型 节约内存空间
        //将不同的数据类型融合到同一段内存里面
        union MyUnion{
            //这三个类型的变量会存储到同一段内存里面
            //size占用最大的字节数的成员所占用的内存的大小 就是float的大小
            int a;
            char b;
            float c;
        };
        
        int main(){
            MyUnion unio;
            printf("a:%#x,b:%#x,c:%#x\n", &unio.a, &unio.b, &unio.c);
            //打印结果:(地址是同一个)
            //a:0x18fa34,
            //b:0x18fa34,
            //c:0x18fa34
        
            unio.a = 10;
            unio.b = 'a';
            unio.c = 1.2f;
            printf("a: %d, b: %c, c: %f\n", unio.a, unio.b, unio.c);
            //打印结果:  a: 1067030938, b: ? c: 1.200000
            //只能取到第一个值,使用的时候只能使用最近赋值的那个变量。
        
            system("pause");
            return 0;
        }
    

    六、枚举

        #include "stdafx.h"
        #include <stdlib.h>
        
        enum{
            //默认monday = 0, saturday =1, sunday =2.
            //如果将第一个赋值为10,则后面的值会自动加一赋值  saturday = 11,sunday =12.
            monday = 10,
            saturday,
            sunday,
        };
        
        int main(){
            printf("monday:%d saturday:%d sunday:%d ", monday, saturday, sunday);
            //打印结果:monday:10 saturday:11 sunday:12
            system("pause");
            return 0;
        }
    

    拓展:

    实现一个双向链表,存储的是随机数据(int |char* )。增删改查,并且对双链表数据进行排序。

    相关文章

      网友评论

          本文标题:ndk04_结构体,typedef,公用体,枚举

          本文链接:https://www.haomeiwen.com/subject/tfdkjxtx.html