c语言进阶——结构体专题

结构体类型定义及变量定义

1、定义结构体类型变量的三种方式

//用类型定义变量的方法3种
//第一种:定义类型的同时定义变量
struct Student
{
    char name[64];
    int age;
}s1, s2;

//第二种:使用结构体类型定义变量
typedef struct Student
{
    char name[64];
    int age;
}Student;
int main()
{
    Student s;
}

//第三种:匿名类型定义变量 
struct 
{
    char name[64];
    int age;
}s3, s4; 

2、初始化结构体变量三种方法

//初始化变量的三种方法
//第一种 
typedef struct Teacher
{
    char name[64];
    int age ;
    int id;
}Teacher;
int main()
{
    Teacher  t3 = {"aaaa", 31, 01}; 
} 

//第二种 
struct Student2
{
    char name[64];
    int age ;
}s5 = {"names", 21};
//第三种 
struct 
{
    char name[64];
    int age ;
}s6 = {"names", 21};

3、指针法和变量法操作结构体

typedef struct Teacher
{
    char name[64];
    int age ;
    int id;
}Teacher;
int main()
{

    Teacher  t1;
    Teacher  t2 = {"aaaa", 32, 02};
    //变量形式操作内存空间 
    t1.age = 31;  //.操作符是寻址操作 计算age 相对于t1的字节偏移量(在cpu中进行计算) 
    //通过指针的方式 操作 内存空间
    Teacher *p = NULL;
    p = &t2;
    printf("p->age:%d \n", p->age); //->是寻址操作 算age相对于t2的字节偏移量(在cpu中进行计算) 
    return 0; 
}

结构体数组做函数参数

1、数组法

typedef struct Teacher
{
    char name[64];
    int age ;
    int id;
}Teacher;

//结构体数组退化为一级指针 
void printTeacher(Teacher *array, int num)
{
    int i = 0;

    for (i=0; iprintf("age:%d \n", array[i].age);
    }
}

int main()
{
    int         i = 0;
    Teacher     Array[3];//申请结构体数组
    int         num = 3;
    for (i=0; iprintf("\nplease enter age:");
        scanf("%d", &(Array[i].age) );
    }
    printTeacher(Array, num);
    return 0;
}

2、指针法

typedef struct Teacher
{
    char name[64];
    int age ;
    int id;
}Teacher;

Teacher * createTeacher(int num)
{
    Teacher * tmp = NULL;
    tmp = (Teacher *)malloc(sizeof(Teacher) * num); //相当于Teacher    Array[3] 
    if (tmp == NULL)
    {
        return NULL;
    }
    return tmp; 
}

void FreeTeacher(Teacher *p)
{
    if (p != NULL)
    {
        free(p);
    }
}

int main()
{
    int         i = 0;
    int         num = 3;
    Teacher *pArray = NULL;
    //申请一个结构体指针,并分配num个Teacher内存空间 
    pArray = createTeacher(num);

    for (i=0; i"\nplease enter age:");
        scanf("%d", & (pArray[i].age) );
    }
    printTeacher(pArray, num);
    FreeTeacher(pArray);
    return 0;
}

结构体中的指针模型

1、结构体中嵌套一级指针

typedef struct Teacher
{
    char name[64];
    char *alisname;//结构体中嵌套一级指针 
    int age ;
    int id;
}Teacher;

int createTeacher(Teacher **pT, int num)
{
    int i = 0;
    Teacher * tmp = NULL;
    //先给Teacher类型分配内存空间 
    tmp = (Teacher *)malloc(sizeof(Teacher) * num); 
    if (tmp == NULL)
    {
        return -1;
    }
    memset(tmp, 0, sizeof(Teacher) * num);
    //给每个Teacher类型中的一级指针分配内存空间 
    for (i=0; i.alisname = (char *)malloc(60);
    }

    *pT = tmp; //二级指针 形参 去间接的修改 实参 的值 
    return 0; 
}

//释放内存时从里向外释放 
void FreeTeacher(Teacher *p, int num)
{
    int i = 0;
    if (p == NULL)
    {
        return;
    }
    for (i=0; iif (p[i].alisname != NULL)
        {
            free(p[i].alisname);
        }
    }
    free(p);
}

int main()
{
    int         ret = 0;
    int         i = 0;
    int         num = 3;
    Teacher *pArray = NULL;
    ret = createTeacher(&pArray, num);
    if (ret != 0)
    {
        printf("func createTeacher02() er:%d \n ", ret);
        return ret;
    }
    FreeTeacher(pArray, num);
    pArray = NULL; 
    return 0;
}

2、结构体中嵌套二级指针

typedef struct Teacher
{
    char name[64];
    char *alisname;//一级指针嵌套 
    char **stuname;//二级指针嵌套 
    int age ;
    int id;
}Teacher;

int createTeacher(Teacher **pT, int num)
{
    int i = 0, j = 0;
    Teacher * tmp = NULL;
    //Teacher分配内存空间 
    tmp = (Teacher *)malloc(sizeof(Teacher) * num); //  Teacher     Array[3]
    if (tmp == NULL)
    {
        return -1;
    }
    memset(tmp, 0, sizeof(Teacher) * num);

    for (i=0; ichar **p = NULL;

        //给Teacher中的一级指针分配内存空间 
        tmp[i].alisname = (char *)malloc(60);

        //给Teacher中的二级指针分配内存空间 
        p = (char **)malloc(3 * sizeof(char *));  //打造二维内存
        for (j=0; j<3; j++)
        {
            p[j] = (char *)malloc(120);
        }
        tmp[i].stuname = p; 
    }
    *pT = tmp;  //二级指针 形参 去间接的修改 实参 的值 
    return 0; //
}

void FreeTeacher(Teacher *p, int num)
{
    int i = 0, j = 0;
    if (p == NULL)
    {
        return;
    }
    for (i=0; i//释放一级指针
        if (p[i].alisname != NULL)
        {
            free(p[i].alisname);
        }

        //释放二级指针
        if (p[i].stuname != NULL)
        {
            char **myp = p[i].stuname ;
            for (j=0; j<3; j++)
            {
                if (myp[j] != NULL)
                {
                    free(myp[j]);
                }
            }
            free(myp);
            p[i].stuname = NULL;
        }
    }
    free(p);
}

int main()
{
    int         ret = 0;
    int         i = 0, j = 0;
    int         num = 3;
    Teacher *pArray = NULL;
    ret = createTeacher(&pArray, num);
    if (ret != 0)
    {
        printf("func createTeacher02() er:%d \n ", ret);
        return ret;
    }
    FreeTeacher(pArray, num);
    pArray = NULL;
    return 0;
}

你可能感兴趣的:(C语言进阶)