结构体做函数参数的进阶:嵌套一二级指针

 

嵌套一级指针

#define  _CRT_SECURE_NO_WARNINGS 
#include 
#include 
#include 

#define  NAME_LEN		(64)

struct teacher
{
	int id;
	char *name;
};


int create_teachers(struct teacher **tpp, int num)
{
	struct teacher *tp = *tpp;
	int i = 0;

	if (tpp == NULL) {
		return -1;
	}

	tp = (struct teacher*)malloc(sizeof(struct teacher) *num);
	if (tp == NULL) {
		fprintf(stderr, "malloc tp error\n");
		return -1;
	}
	memset(tp, 0, sizeof(struct teacher) * num);

	for (i = 0; i < num; i++) {
		tp[i].name = (char*)malloc(sizeof(char)*NAME_LEN);
		memset(tp[i].name, 0, sizeof(char)*NAME_LEN);
	}

	*tpp = tp;

	return 0;
}

void print_teacher(struct teacher* p, int num)
{
	int i = 0;

	for (i = 0; i < num; i++) {
		printf("----\n");
		printf("id : %d\n", p[i].id);
		printf("name: %s\n", p[i].name);
	}
}

void sort_teacher(struct teacher *tp, int num)
{
	int i = 0;
	int j = 0;
	struct teacher temp_teacher;

	for (i = 0; i < num; i++) {
		for (j = i; j < num; j++) {
			if (tp[i].id > tp[j].id) {
				temp_teacher = tp[i];
				tp[i] = tp[j];
				tp[j] = temp_teacher;
			}
		}
	}
}

void free_teachers(struct teacher **tpp, int num)
{
	struct teacher *tp = *tpp;
	int i = 0;

	if (tpp == NULL) {
		return ;
	}

	if (tp != NULL) {
		for (i = 0; i < num; i++) {
			if (tp[i].name != NULL) {
				free(tp[i].name);
				tp[i].name = NULL;
			}
		}

		free(tp);
		*tpp = NULL;
	}

	return;
}


int main(void)
{
	struct teacher *tp = NULL;
	int num = 2;
	int i = 0;
	int ret = 0;

	ret = create_teachers(&tp, num);
	if (ret < 0) {
		return -1;
	}

	for (i = 0; i < num; i++) {
		printf("enter tp[%d]'s id  ", i);
		scanf("%d", &tp[i].id);
		printf("enter tp[%d]'s name ", i);
		scanf("%s", tp[i].name);
	}

	print_teacher(tp, num);

	sort_teacher(tp, num);
	printf("after sort\n");

	print_teacher(tp, num);

	free_teachers(&tp, num);

	if (tp == NULL) {
		printf("freee succ!\n");
	}


	return 0;
}

嵌套二级指针

向指针所指的内存空间中copy数据,需要先分配空间。

#define  _CRT_SECURE_NO_WARNINGS 
#include 
#include 
#include 

#define NAME_LEN 64

struct teacher
{
	int id;
	char *name;
	char **student_name;
	int stu_num;
};

int create_teacher(struct teacher **tpp, int num)
{
	struct teacher *tp = NULL;
	int i = 0;

	if (tpp == NULL) {
		return -1;
	}

	tp = (struct teacher *)malloc(sizeof(struct teacher) * num);
	if (tp == NULL) {
		fprintf(stderr, "malloc struct teacher * tp error\n ");
		return -1;
	}
	memset(tp, 0, sizeof(struct teacher) *num);


	for (i = 0; i < num; i++) {
		tp[i].name = (char*)malloc(sizeof(char)*NAME_LEN);
		memset(tp[i].name, 0, NAME_LEN);
	}

	*tpp = tp;

	return 0;
}

int create_student(struct teacher *tp)
{
	int i = 0;

	if (tp == NULL) {
		return -1;
	}

	tp->student_name = (char **)malloc(sizeof(char*)* (tp->stu_num) );
	if (tp->student_name == NULL) {
		fprintf(stderr, "malloc tp->studentname error\n");
		return -1;
	}
	memset(tp->student_name, 0, sizeof(char *)* tp->stu_num);

	
	for (i = 0; i < tp->stu_num; i++) {
		tp->student_name[i] = (char*)malloc(sizeof(char)*NAME_LEN);
		memset(tp->student_name[i], 0, sizeof(char)*NAME_LEN);
	}

	return 0;
}

void print_teachers(struct teacher *tp, int num)
{
	int i = 0;
	int j = 0;

	for (i = 0; i < num; i++) {
		//代表一个老师
		printf("=====\n");
		printf("id: %d\n", tp[i].id);
		printf("name : %s\n", tp[i].name);
		printf("stu_name:%d\n", tp[i].stu_num);
		for (j = 0; j < tp[i].stu_num; j++) {
			printf("stu[%d]:%s\n", j, tp[i].student_name[j]);
		}
	}
}

void sort_teachers(struct teacher *tp, int num)
{
	int i = 0;
	int j = 0;
	struct teacher temp_teacher;

	if (tp == NULL) {
		return;
	}

	for (i = 0; i < num; i++) {
		for (j = i; j < num; j++) {
			if (tp[i].id > tp[j].id) {
				temp_teacher = tp[i];
				tp[i] = tp[j];
				tp[j] = temp_teacher;
			}
		}
	}

	return ;
}

void free_teachers(struct teacher **tpp, int num)
{
	struct teacher *tp = *tpp;

	int i = 0;
	int j = 0;

	if (tpp == NULL) {
		return;
	}

	if (tp == NULL) {
		return;
	}

	for (i = 0; i < num; i++) {
		//释放一个老师
		// 线是否老师 名字
		if (tp[i].name != NULL) {
			free(tp[i].name);
			tp[i].name = NULL;
		}

		//释放学生信息
		if (tp[i].student_name != NULL) {

			for (j = 0; j < tp[i].stu_num; j++) {
				if (tp[i].student_name[j] != NULL) {
					free(tp[i].student_name[j]);
					tp[i].student_name[j] = NULL;
				}
			}

			free(tp[i].student_name);
			tp[i].student_name = NULL;
		}
	}

	free(tp);

	*tpp = NULL;
}

int main(void)
{
	int num = 2;
	int i = 0;
	int j = 0;
	struct teacher *tp = NULL;

	//开辟num个teacher空间
	create_teacher(&tp, num);

	for (i = 0; i < num; i++) {
		//代表一个老师
		printf("enter tp[%d]'s id: ", i);
		scanf("%d", &tp[i].id);
		printf("enter tp[%d]'s name:", i);
		scanf("%s", tp[i].name);
		printf("enter tp[%d]'s stu_num", i);
		scanf("%d", &(tp[i].stu_num)  );
		create_student(&tp[i]);
		for (j = 0; j < tp[i].stu_num; j++) {
			printf("enter tp[%d]->std[%d]'name: ", i, j);
			scanf("%s", tp[i].student_name[j]);
		}
	}

	print_teachers(tp, num);

	sort_teachers(tp, num);

	printf(" after sort\n");

	print_teachers(tp, num);

	free_teachers(&tp, num);

	if (tp == NULL) {
		printf("tp == NULL\n");
	}

	return 0;
}

 

 

 

你可能感兴趣的:(C/C++)