#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
//define结构体
//定义一个数据类型 固定大小内存块的别名 还没有分配内存
//重定义
typedef struct Teacher
{
char name[64];
int age;
int id;
}teacher;
//用类型定义变量
void main()
{
//struct Teacher t1;//告诉编译器给我分配内存
teacher t1;
printf("hello\n");
return;
}
from拷贝到to里 和t1 ,t2 无关
结构体元素做函数参数和结构体指针做函数参数
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
//define结构体
//定义一个数据类型 固定大小内存块的别名 还没有分配内存
//重定义
typedef struct Teacher
{
char name[64];
int age;
int id;
}teacher;
//用类型定义变量
//初始化
//void copyTeacher(teacher to, teacher from)
//{
// to = from;
//}
void copyTeacher(teacher *to, teacher *from)//把from的首地址扔给to
{
*to = *from;
}
void printTeacher(teacher* array, int num)//数组做函数参数退化成指针
{
int i = 0; num = 3;
for (int i = 0; i < num; i++)
{
printf("age:%d\n", array[i].age);
}
}
//结构体数组 3 输入老师年龄排序
void main()
{
//struct Teacher t1;//告诉编译器给我分配内存
teacher t1 = {"aaaa",12,1111333};//初始化1
teacher t2 ={ "alaa",19,11991333 };;
//通过指针 方式操作内存
/*teacher *p = NULL;
p = &t1;
printf("p->age:%d\n",p->age);*/
copyTeacher(&t2, &t1);//把t2 赋值给t2 地址
printf("p->age:%d\n",t2.age );
int i = 0;
int num = 3;
teacher Array[3];
for (i = 0; i < num; i++)
{
printf("\n please enter age:");
scanf("%d",&(Array[i].age));
}
//print age
printTeacher(Array, num);
printf("hello\n");
return;
}
函数封装
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
//define结构体
//定义一个数据类型 固定大小内存块的别名 还没有分配内存
//重定义
typedef struct Teacher
{
char name[64];
int age;
int id;
}teacher;
//用类型定义变量
//初始化
//void copyTeacher(teacher to, teacher from)
//{
// to = from;
//}
void copyTeacher(teacher *to, teacher *from)//把from的首地址扔给to
{
*to = *from;
}
void printTeacher(teacher* array, int num)//数组做函数参数退化成指针
{
int i = 0; num = 3;
for (int i = 0; i < num; i++)
{
printf("age:%d\n", array[i].age);
}
}
void sortTeacher(teacher *array, int num)//按照年龄排序
{
int i, j;
teacher tmp;
for (int i = 0; i < num; i++)
{
for (j = i + 1; j < num; j++)
{
if (array[i].age > array[j].age)
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
}
//在mallloc上分配内存
//void creatTeacher(int num) //改进:返回一个节点 因为teacher是一个复杂的数据类型 一开始就是个一级指针
teacher* creatTeacher(int num)
{
teacher* tmp = NULL;
tmp = (teacher*)malloc(sizeof(teacher) * num);// teacher Array[3]
if (tmp == NULL)
{
return NULL;
}
}
//free
void FreeTeacher(teacher* p)
{
if (p != NULL)
{
free(p);
}
}
//结构体数组 3 输入老师年龄排序
int main()
{
//struct Teacher t1;//告诉编译器给我分配内存
teacher t1 = {"aaaa",12,1111333};//初始化1
teacher t2 ={ "alaa",19,11991333 };;
//通过指针 方式操作内存
/*teacher *p = NULL;
p = &t1;
printf("p->age:%d\n",p->age);*/
copyTeacher(&t2, &t1);//把t2 赋值给t2 地址
printf("p->age:%d\n",t2.age );
int i = 0;
int num = 3;
//teacher Array[3];//在临时区分配内存 stack
teacher* pArray = NULL;
pArray = creatTeacher(num);//把地址接过来 内存空间有了 接下来使用内存空间
/*for (i = 0; i < num; i++)
{
printf("\n please enter age:");
scanf("%d",&(Array[i].age));
}*/
for (i = 0; i < num; i++)
{
printf("\n please enter age:");
scanf("%d", &(pArray[i].age));
}
//print age
printf("before");
//printTeacher(Array, num);//数组名代表首元素地址
printTeacher(pArray, num);
//sortTeacher(Array, num);
sortTeacher(pArray, num);
printf("after");
printTeacher(pArray, num);
FreeTeacher(pArray);
printf("hello\n");
return;
}