结构体的基本操作

2022-10-08  本文已影响0人  arkliu

结构体定义和初始化

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 1. 结构体类型的定义
//struct关键字
//struct Teacher合在一起才是类型
struct Teacher
{
    char name[50];
    int age;
};

//3. 定义类型的同时,定义变量
struct Student
{
    char name[50];
    int age;
}st1, st2;

// 5. 通过typedef改类型名字
typedef struct People {
    char name[50];
    int age;
} People;

int main() {
    // 2. 结构体变量的声明
    struct Teacher teacher;

    // 4.结构体变量初始化
    // 4.1定义变量时候直接初始化,通过{}
    struct Teacher teacher1 = {"zhangsan", 22};
    printf("name = %s  age = %d\n", teacher1.name, teacher1.age); //name = zhangsan  age = 22

    People people;
    strcpy(people.name, "wangwu");
    people.age = 34;
    printf("name = %s  age = %d\n", people.name, people.age);

    struct Teacher * teacher2 = malloc(sizeof(struct Teacher));
    strcpy(teacher2->name, "wangwu");
    teacher2->age = 34;
    printf("name = %s  age = %d\n", teacher2->name, teacher2->age);
   
}

结构体变量相互赋值

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct People {
    char name[50];
    int age;
} People;

void copyPeople(People *dest, People source) {
    dest->age = source.age;
    strcpy(dest->name,source.name);
}

int main() {
    People people = {"李四", 23};
    //相同类型的两个结构体变量可以相互赋值
    //把people成员变量内存的值拷贝到people2成员变量的内存
    People people2 = people;
    printf("name = %s  age = %d\n", people2.name, people2.age);
   
    People p3;
    memset(&p3, 0, sizeof(p3));
    copyPeople(&p3, people);
    printf("name = %s  age = %d\n", p3.name, p3.age);
}

结构体数组

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct People {
    char name[50];
    int age;
} People;

int main() {
    People p[3] = {
        {"abc", 22},
        {"def", 33},
        {"ghi", 44}
    };
    //静态
    People p2[3] = {"abc", 22,"def", 33,"ghi", 44};
    for (size_t i = 0; i < 3; i++)
    {
        printf("name = %s  age = %d\n", p2[i].name, p2[i].age);
    }
    
    //动态
    People * p3 = malloc(3 * sizeof(People));
    char buf[128];
    for (size_t i = 0; i < 3; i++)
    {
        sprintf(buf, "name%d%d", i, i);
        strcpy(p3[i].name, buf);
        p3[i].age = i;
    }

    for (size_t i = 0; i < 3; i++)
    {
        printf("name = %s  age = %d\n", p3[i].name, p3[i].age);
    }
    if (p3 != NULL)
    {
        free(p3);
        p3 = NULL;
    }
}

结构体嵌套一级指针

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct People {
    char * name;
    int age;
} People;

int main() {
    People p1;
    p1.age = 22;
    p1.name = (char *)malloc(30 * sizeof(char));
    strcpy(p1.name, "张三");
    printf("p1 name = %s age = %d\n", p1.name, p1.age); //p1 name = 张三 age = 22
    if (p1.name != NULL)
    {
        free(p1.name);
        p1.name = NULL;
    }


    People * p2;
    p2 = (People *)malloc(sizeof(People));
    p2->age = 33;
    p2->name = (char *)malloc(30 * sizeof(char));
    strcpy(p2->name, "李四");
    printf("p2 name = %s age = %d\n", p2->name, p2->age);
    if (p2->name != NULL)
    {
        free(p2->name);
        p2->name = NULL;
    }
    if (p2 != NULL)
    {
        free(p2);
        p2 = NULL;
    }
    
    
    People * p3;
    p3 = (People *)malloc(sizeof(People) * 3);
    char buf[128];
    for (size_t i = 0; i < 3; i++)
    {
        p3[i].age = i;
        p3[i].name = malloc(sizeof(char) * 30);
        sprintf(buf, "name %d %d", i, i);
        strcpy(p3[i].name, buf);
    }
    for (size_t i = 0; i < 3; i++)
    {
        printf("p3 name = %s  age = %d\n", p3[i].name, p3[i].age);
    }
    for (size_t i = 0; i < 3; i++)
    {
        if (p3[i].name != NULL)
        {
            free(p3[i].name);
            p3[i].name = NULL;
        }
    }
    if (p3 != NULL)
    {
        free(p3);
        p3 = NULL;
    } 
}

结构体嵌套二级指针

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STU_SIZE 3

//一个老师对应多个学生
typedef struct Teacher {
    char ** stu;
} Teacher;

int main() {
    // init
    Teacher t1;
    t1.stu = (char **)malloc(sizeof(char *) * STU_SIZE);
    char buf[128];
    for (size_t i = 0; i < STU_SIZE; i++)
    {
        t1.stu[i] = (char *)malloc(sizeof(char) * 30);
        sprintf(buf, "name %d%d", i,i);
        strcpy(t1.stu[i], buf);
    }
    // print
    for (size_t i = 0; i < STU_SIZE; i++)
    {
        printf("name = %s\n", t1.stu[i]);
    }

    //free
    for (size_t i = 0; i < STU_SIZE; i++)
    {
        if (t1.stu[i] != NULL)
        {
            free(t1.stu[i]);
            t1.stu[i] = NULL;
        }
    }
    if (t1.stu != NULL)
    {
        free(t1.stu);
        t1.stu = NULL;
    }

    // init
    Teacher * t2;
    t2 = (Teacher *)malloc(sizeof(Teacher));
    t2->stu = (char **)malloc(sizeof(char *) * STU_SIZE);
    for (size_t i = 0; i < STU_SIZE; i++)
    {
        t2->stu[i] = (char *)malloc(sizeof(char) * 30);
        sprintf(buf, "name %d%d", i,i);
        strcpy(t2->stu[i], buf);
    }
    // print
    for (size_t i = 0; i < STU_SIZE; i++)
    {
        printf("name = %s\n", t2->stu[i]);
    }
    // free
    for (size_t i = 0; i < STU_SIZE; i++)
    {
        if (t2->stu[i] != NULL)
        {
            free(t2->stu[i]);
            t2->stu[i] = NULL;
        }
    }
    if (t2->stu != NULL)
    {
        free(t2->stu);
        t2->stu = NULL;
    }
    if (t2 != NULL)
    {
        free(t2);
        t2 = NULL;
    }
    
    return 0;
}

结构体偏移量

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STU_SIZE 3

//结构体类型定义下来,内部的成员变量的内存布局已经确定
typedef struct Teacher {
    char name[64]; // 64
    int age; // 4
    int id; // 4
} Teacher;

int main() {
    Teacher t1;
    Teacher * t2 = NULL;
    t2 = &t1;

    int n1 = (int)(&t2->age) - (int)t2; // 相对于结构体首地址
    printf("n1 = %d\n", n1); // n1 = 64
    
    return 0;
}

联合体

将几种不同类型的变量存放到同一段内存中,这种数据结构称为联合体

#include <stdio.h>

int main() {
    union TestUnion
    {
        int a;
        float b;
        char c;
    } un={10};

    printf("a: %d  b:%f   c: %c", un.a, un.b, un.c);
    un.b = 8.8f;
    printf("a: %d  b:%f   c: %c", un.a, un.b, un.c);
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读