【C语言】结构体详解
文章目录
前言
本篇文章会讲到结构体的基本使用,结构体的深浅拷贝,结构体嵌套一级指针练习,结构体嵌套二级指针练习,结构体偏移量,内存对齐的有关知识。
一、结构体的基本使用
代码中讲述了如何定义结构体,以及哟个typedef给结构体起别名,如何创建结构体,在堆区和栈上的不同创建形式,以及在栈上和堆区里分配内存。
c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//用typedef起别名
//struct Person
//{
// char name[64];
// int age;
//};
//typedef struct Person myPerson
typedef struct Person
{
char name[64];
int age;
}myPerson;
void test01()
{
struct Person p = { "Tom",18 };
myPerson p2 = { "Jerry",20 };
}
struct Person2
{
char name[64];
int age;
}myPerson2 = { "aaa",20 };
void test02()
{
printf("姓名:%s 年龄:%d", myPerson2.name, myPerson2.age);
}
//匿名结构体
struct
{
char name[64];
int age;
}myPerson3 = { "bbb",13 };
void test03()
{
printf("姓名:%s 年龄:%d", myPerson3.name, myPerson3.age);
}
//结构体的创建
void test04()
{
//创建在栈上
struct Person p = { "aaa",10 };
printf("姓名:%s 年龄:%d", p.name, p.age);
//创建在堆区
struct Person* p2 = malloc(sizeof(struct Person));
strcpy(p2->name, "bbb");
p2->age = 18;
printf("姓名:%s 年龄:%d", p2->name, p2->age);
if (p2!= NULL)
{
free(p2);
p2 = NULL;
}
}
void printArray(struct Person personArray[],int len)
{
for (int i = 0; i < len; i++)
{
printf("姓名:%s,年龄:%d\n", personArray[i].name, personArray[i].age);
}
}
//结构体变量数组创建
void test05()
{
//在栈上分配内存
struct Person persons[] = {
{"aaa",18},
{"bbb",19},
{"ccc",20},
{"ddd",21},
};
int len = sizeof(persons) / sizeof(struct Person);
//printArray(persons,len);
//在堆区分配内存
struct Person* pArray = malloc(sizeof(struct Person) * 4);
for (int i = 0; i < 4; i++)
{
sprintf(pArray[i].name, "name_%d", i + 1);
pArray[i].age = 18 + i;
}
printArray(pArray, 4);
if (pArray != NULL);
{
free(pArray);
pArray = NULL;
}
}
int main()
{
//test01();
//test02();
//test03();
//test04();
test05();
return 0;
}
二、结构体的深浅拷贝
c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Person
{
char name[64];
int age;
};
void test1()
{
struct Person p1 = { "Tom",18 };
struct Person p2 = { "Jerry",20 };
p1 = p2;//浅拷贝,按字节拷贝
printf("p1的姓名: %s 年龄:%d\n", p1.name, p1.age);
printf("p2的姓名: %s 年龄: %d\n", p2.name, p2.age);
}
struct Person2
{
char* name;
int age;
};
void test2()
{
struct Person2 p1;
p1.name = malloc(sizeof(char) * 64);
strcpy(p1.name, "Tom");
p1.age = 18;
struct Person2 p2;
p2.name = malloc(sizeof(char) * 128);
strcpy(p2.name, "Jerry");
p2.age = 20;
//p1=p2; //系统提供的复制操作是简单的浅拷贝,我们需要做手动复制,提供深拷贝
/手动赋值/
//先释放原来的堆区内容
if (p1.name != NULL)
{
free(p1.name);
p1.name = NULL;
}
//在堆区创建内存
p1.name = malloc(strlen(p2.name) + 1);
strcpy(p1.name, p2.name);
p1.age = p2.age;
/
printf("p1的姓名:%s 年龄:%d\n", p1.name, p1.age);
printf("p2的姓名:%s 年龄:%d\n", p2.name, p2.age);
if (p1.name != NULL)
{
free(p1.name);
p1.name = NULL;
}
if (p2.name != NULL)
{
free(p2.name);
p2.name = NULL;
}
}
int main()
{
test1();
test2();
return 0;
}
三、结构体嵌套一级指针
c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Person
{
char* name;
int age;
};
struct Person** allocateSpace()
{
struct Person** temp = malloc(sizeof(struct Person*) * 3);
for (int i = 0; i < 3; i++)
{
//创建结构体内存
temp[i] = malloc(sizeof(struct Person));
//将结构体姓名 创建在堆区
temp[i]->name = malloc(sizeof(char) * 64);
//给姓名赋值
sprintf(temp[i]->name, "name_%d", i + 1);
temp[i]->age = 18 + i;
}
return temp;
}
void printPerson(struct Person** pArray, int len)
{
for (int i = 0; i < len; i++)
{
printf("姓名:%s,年龄:%d\n", pArray[i]->name, pArray[i]->age);
}
}
void freeSpace(struct Person** pArray, int len)
{
if (pArray == NULL)
{
return;
}
if (len <= 0)
{
return;
}
for (int i = 0; i < 3; i++)
{
if (pArray[i]->name != NULL)
{
printf("%s被释放了\n", pArray[i]->name);
free(pArray[i]->name);
pArray[i]->name = NULL;
}
free(pArray[i]);
pArray[i] = NULL;
}
}
void test07()
{
struct Person** pArray = NULL;
pArray = allocateSpace();
//打印数组
printPerson(pArray, 3);
//释放内存
freeSpace(pArray,3);
pArray = NULL;
}
int main() {
test07();
system("pause");
return EXIT_SUCCESS;
}
四、结构体嵌套二级指针
c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Teacher
{
char* name;
char** students;
};
void allocateSpace02(struct Teacher*** teachers)
{
if (teachers == NULL)
{
return;
}
//开辟内存
struct Teacher**ts=malloc(sizeof(struct Teacher*) * 3);
//给每个老师分配内存
for (int i = 0; i < 3; i++)
{
ts[i] = malloc(sizeof(struct Teacher));
//给老师的姓名分配内存
ts[i]->name = malloc(sizeof(char) * 64);
//给老师起名字
sprintf(ts[i]->name, "Teacher_ % d", i + 1);
//给学生的数组分配内存
ts[i]->students = malloc(sizeof(char*)*4);
//给学生的姓名开辟内存 以及赋值
for (int j = 0; j < 4; j++)
{
ts[i]->students[j] = malloc(sizeof(char) * 64);
sprintf(ts[i]->students[j], "%s_Student_%d", ts[i]->name, j + 1);
}
}
*teachers = ts;
}
void printTeachers(struct Teacher** pArray)
{
if (pArray == NULL)
{
return;
}
for (int i = 0; i < 3; i++)
{
printf("%s\n", pArray[i]->name);
for (int j = 0; j < 4; j++)
{
printf(" %s\n", pArray[i]->students[j]);
}
}
}
void freeSpace02(struct Teacher** pArray)
{
if (pArray == NULL)
{
return;
}
for (int i = 0; i < 3; i++)
{
//先释放老师姓名
if (pArray[i]->name != NULL)
{
free(pArray[i]->name);
pArray[i]->name = NULL;
}
//释放学生姓名
for (int j = 0; j < 4; j++)
{
if (pArray[i]->students[j] != NULL)
{
free(pArray[i]->students[j]);
pArray[i]->students[j] = NULL;
}
}
//释放学生数组
if (pArray[i]->students != NULL)
{
free(pArray[i]->students);
pArray[i]->students = NULL;
}
//释放老师
if (pArray[i] != NULL)
{
free(pArray[i]);
pArray[i] = NULL;
}
}
//释放老师数组
if (pArray != NULL)
{
free(pArray);
pArray = NULL;
}
}
void test08()
{
struct Teacher** pArray = NULL;
//开辟内存
allocateSpace02(&pArray);
//打印数组
printTeachers(pArray);
//释放数组
freeSpace02(pArray);
pArray = NULL;
}
int main()
{
test08();
return 0;
}
五、结构体的偏移量
#include <stddef.h>
offsetof(struct Teacher, b)
c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <stddef.h>
struct Teacher
{
char a; //0 ~ 3
int b; //4 ~ 7
};
void test01()
{
struct Teacher t1;
struct Teacher* p = &t1;
printf("b的属性偏移量为:%d\n", (int)&(p->b) - (int)p);
printf("b的属性偏移量为:%d\n", offsetof(struct Teacher, b));
}
//通过偏移量 操作内存
void test02()
{
struct Teacher t1 = { 'a', 10 };
printf("t1.b = %d\n", *(int*)((char*)&t1 + offsetof(struct Teacher, b)));
printf("t1.b = %d\n", *(int*)((int*)&t1 + 1));
}
struct Teacher2
{
char a;
int b;
struct Teacher c;
};
void test03()
{
struct Teacher2 t1 = { 'a', 10, 'b', 20 };
int offset1 = offsetof(struct Teacher2, c);
int offset2 = offsetof(struct Teacher, b);
printf("%d\n", *(int*)((char*)&t1 + offset1 + offset2));
printf("%d\n", ((struct Teacher*)((char*)&t1 + offset1))->b);
}
int main() {
//test01();
//test02();
test03();
system("pause");
return EXIT_SUCCESS;
}
六、内存对齐
#pragma pack(show) //查看当前对齐模数 ,对齐模数是可以改的,改成2的N次方
内存对齐要求:
第一个属性开始 从0开始偏移
第二个属性开始 要放在 该类型的大小 与 对齐模数比 取小的值 的整数倍
所有属性都计算完后,再整体做二次偏移,将整体计算的结果 要放在 结构体最大类型 与对齐模数比 取小的值的 整数倍上
c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#pragma pack(show) //查看当前对齐模数 ,对齐模数是可以改的,改成2的N次方
typedef struct _STUDENT {
int a; //0 ~ 3
char b; //4 ~ 7
double c; //8 ~ 15
float d; //16 ~ 19
}Student;
void test09()
{
printf("sizeof student = %zd\n", sizeof(Student));//24
}
//结构体嵌套结构体时候,子结构体放在该结构体中最大类型 和对齐模数比 的整数倍上即可
typedef struct _STUDENT2 {
char a; // 0 ~ 7
Student b; // 8 ~ 31
double c; //32 ~ 39
}Student2;
void test10()
{
printf("sizeof student = %d\n", sizeof(Student2));
}
int main() {
test09();
test10();
system("pause");
return EXIT_SUCCESS;
}
总结
到这里这篇文章的内容就结束了,谢谢大家的观看,如果有好的建议可以留言喔,谢谢大家啦!