C 结构体 typedef (取别名) 共用体union 枚举enum

=========结构体定义 ========

//一系列不同类型的数据类型
//类型!=变量,结构体不分配内存
struct Student{
    char name[20];
    int age;

}Lucy;
//全局变量
int aaa;
int bbb;
//锁定结构体数量
struct {
    char name[20];
    int age;
    char gender[10];
    int classId;

}stu33,stu44;

//结构体定义和初始化
int main(){
    struct Student stu1 = { "你知不知道", 25};
    struct Student stu2;
    strcpy(stu2.name,"何以笙箫默");
    stu1.age = 20;
    printf("%s,%d\n", stu1.name, stu1.age);


    system("pause");
    return 0;
}

=======//结构体的数组======

struct Student{
    char name[20];
    int age;

}Lucy;

//结构体的数组
int main(){
    int i;
    //大括号可以加,也可以不加,最好加上
    struct Student stu1[3] = { { "曾经仗剑走天涯", 20 }, { "程序员", 25 }, { "bug太多", 23 } };
    for (i = 0; i < 3; i++)
    {
        printf("%s,%d\n", stu1[i].name, stu1[i].age);
    }
    struct Student stu2[5];

    for ( i = 0; i < 5; i++)
    {
        stu2[i].age = 20 + i;
        strcpy(stu2[i].name, "日光浴");
        printf("%s,%d\n", stu2[i].name, stu2[i].age);
    }

    struct Student *stu=stu1;
    for (i = 0; i < 3; i++)
    {
        printf("%s  , %d\n", stu1[i].name, stu1[i].age);
    }

    struct Student *stud;
    stud = (Student *)malloc(sizeof(struct Student) * 4);
    //你需要赋值的指针,赋值成什么,空间多大
    memset(stud, 0, sizeof(struct Student) * 4);
    for (i = 0; i < 4; i++)
    {
        (stud+i)->age = 20 + i;
        strcpy((stud + i)->name, "水手平");
        //stud[i].age = 20+i ;
        //strcpy(stud[i].name, "nihao");
        printf("%s, %d\n", stud[i].name, stud[i].age);
    }

    system("pause");
    return 0;
}

======结构体中添加结构体指针成员变量  单链表=======

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 int _in;
typedef char *string;
typedef int(*PFI) (char *, char *);
int fun(char *, char *){
    return 0;
}
//二叉树
typedef Tnode * Treeptr;
typedef struct Tnode{
    char * word;
    int count;
    //Tnode *left; 
    //Tnode *right;
    Treeptr left;
    Treeptr right;
} BinaryTreeNode;

int main(){
    _in a = 20;
    printf("%d \n", a);
    string str;
    str = "hello world";
    char *ch;
    ch = "hello world";
    printf("%s ,%s \n ", ch, str);

    char  *p;
    PFI pf;
    pf = fun;
    pf(p, p);

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


    system("pause");
    return 0;
}

=======共用体======

//将不同类型的数据类型放到同一段内存中
//共用一段内存,当然节省内存
union MyUnion{
    int a;
    char b;
    float c;
};
int main(){
    MyUnion un;
    printf("%#x,%#x,%#x\n",&un.a,&un.b,&un.c);
    un.a = 10;
    un.b= 's';
    un.c = 10.2;
    printf("%d,%c,%f\n", un.a,un.b, un.c);
    system("pause");
    return 0;
}

=====//枚举  c语言中直接使用=======

enum{
    sunday,monday,
    tudesday,wendendsday

};

int main(){
    printf("%d\n",sunday);
    int a=10;
    int *p;
    p=&a ;
    int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    int *p1;
    *p1 = a;

    system("pause");

    return  0;
}

猜你喜欢

转载自blog.csdn.net/lolyiku/article/details/81231099
今日推荐