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

一、结构体定义和初始化

#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* )。增删改查,并且对双链表数据进行排序。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容