结构体进阶
c
结构体的基础操作
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//1.自己定义一个数据类型,数据类型的本质是固定大小内存块的别名
//2..c和.cpp定义结构体类型定义变量的时候,c和c++编译器的处理行为不一样
//3.结构体类型typedef
//4.结构变量内存四字节存放,
typedef struct Teacher
{
char name[62];
int age;
char title[128];
/************************************************************************/
/*
char name[62]; // 62字节
char c1; //1字节
char c2; //1字节
int age; //4字节
一共68字节 */
/************************************************************************/
/************************************************************************/
/*
char name[62]; //62字节
char c1; //63 + 1空闲字节
int age; //4字节
char c2; //1字节 + 3空闲字节
一共72字节 */
/************************************************************************/
}Teacher;
//结构体变量的初始化3种方法
//结构体定义变量的4种方法
//结构体变量的复制方法,操作结构体变量
void main()
{
//告诉编译器要分配内存
struct Teacher t1; /*使用struct Teacher直接定义*/
Teacher t2 = { "dddd", 40, "dffd" };/*只有使用了tyededef重命名了结构体才能这样用*/
/*常用的初始化方法就上面一种*/
printf("sizeof(char):%d\n", sizeof(char));
t1.age = 10; /*通过.直接赋值*/
Teacher *p = NULL; /*通过指针间接赋值*/
p = &t1;
p->age = 20;
printf("sizeof(Teacher):%d\n", sizeof(t1));
system("pause");
}
结构体进阶
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Teacher
{
char name[62];
int age;
}Teacher;
void printfArray(Teacher *p, int count)
{
for (int i = 0; i < count; ++i)
{
printf("%d\n", p[i].age);
}
}
Teacher *createTarray(int count)
{
Teacher *p = (struct Teacher *)malloc(count * sizeof(Teacher));
if (p == NULL)
{
return;
}
for (int i = 0; i < count; ++i)
{
memset(&p[i], 0, sizeof(Teacher));
//或者 memset(p1 + i, 0, sizeof(Teacher));
}
return p;
}
int sortArray(Teacher *p, int num)
{
Teacher tmp;
for (int i = 0; i < num; ++i)
{
for (int j = i + 1; j < num; ++j)
{
if (p[i].age < p[j].age)
{
memcpy(&tmp, &p[i], sizeof(Teacher));
memcpy(&p[i], &p[j], sizeof(Teacher));
memcpy(&p[j], &tmp, sizeof(Teacher));
}
}
}
return 0;
}
void freeMem(Teacher *p, int count)
{
if (p != NULL)
{
free(p);
p = NULL; //垃圾,写了和没写一个样
}
// for (int i = 0; i < count; ++i)
// {
// if (p[i] != NULL)
// {
// free(p[i]);
// }
// }
}
void main()
{
Teacher tArray[10];
Teacher *pArray = createTarray(4); //在堆上分配内存
for (int i = 0; i < 4; ++i)
{
printf("请输入age:");
scanf_s("%d", &tArray[i].age); //要改变实参的值
}
printfArray(tArray, 4);
sortArray(tArray, 4);
printfArray(tArray, 4);
system("pause");
}
包含指针的结构体
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Teacher
{
char name[62];
char *a_name;
int age;
}Teacher;
void printfArray(Teacher *p, int count)
{
for (int i = 0; i < count; ++i)
{
printf("%s %d\n", p[i].a_name, p[i].age);
}
}
Teacher *createTarray(int count)
{
Teacher *p = (struct Teacher *)malloc(count * sizeof(Teacher));
if (p == NULL)
{
return NULL;
}
for (int i = 0; i < count; ++i)
{
memset(&p[i], 0, sizeof(Teacher));
p[i].a_name = (char *)malloc(128 * sizeof(char));
memset(p[i].a_name, 0, 128 * sizeof(char));
//或者 memset(p1 + i, 0, sizeof(Teacher));
}
return p;
}
int sortArray(Teacher *p, int num)
{ //按照年龄排序
Teacher tmp;
for (int i = 0; i < num; ++i)
{
for (int j = i + 1; j < num; ++j)
{
if (p[i].age < p[j].age)
{
memcpy(&tmp, &p[i], sizeof(Teacher));
memcpy(&p[i], &p[j], sizeof(Teacher));
memcpy(&p[j], &tmp, sizeof(Teacher));
}
}
}
return 0;
}
void freeMem(Teacher *p, int count)
{
if (p == NULL)
{
return;
}
for (int i = 0; i < count; ++i)
{
if (p[i].a_name != NULL)
{
free(p[i].a_name);
}
}
if (p != NULL) //这里特别小心,怎样分配,怎样释放
{
free(p);
p = NULL; //垃圾代码,写了和没写一个样
}
}
void main()
{
Teacher tArray[10];
Teacher *pArray = createTarray(4); //在堆上分配内存
for (int i = 0; i < 4; ++i)
{
printf("请输入别名:");
scanf_s("%s", pArray[i].a_name); //要改变实参的值
printf("请输入age:");
scanf_s("%d", &tArray[i].age); //要改变实参的值
}
printfArray(tArray, 4);
sortArray(tArray, 4);
printfArray(tArray, 4);
freeMem(pArray, 4); //释放堆上的数据
system("pause");
}