04 数组类型、数组指针类型、数组指针类型变量

数组类型、数组指针类型、数组指针类型变量

数组别名

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef unsigned int u32;


int main(void)
{
	//int a[10];

	typedef int (ARRAY_INT_10) [10]; //为 int【10】这种数组 起一个别名 ARRAY_INT_10

	ARRAY_INT_10 b_array; //int b_array[10];  int *a    int (*a)[10] = &b_array;
	int i = 0;

	for (i = 0; i < 10; i++)
	{
		b_array[i] = i;
	}

	for (i = 0; i < 10; i++) {
		printf("%d\n", b_array[i]);
	}

	//(int[4]) (* pointer);
	int(*pointer)[4];

	ARRAY_INT_10 * p = &b_array;

	printf("p : %d, p+1 : %d", p, p + 1);




	return 0;
}

在这里插入图片描述

多维数组做函数参数会退化为指针

main函数参数

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 操作系统分配了argc 和 argv 和 env 分配的内存
// argc 是输入名字加arv的数目
// argv 可以读取命令行的输入
// env 是环境变量
int main(int argc, char *argv[], char **env)
{
	int i = 0;

	printf(" ------- argv -------\n");
	for (i = 0; i < argc; i++) {
		printf("%s\n", argv[i]);
	}
	printf(" ------- ---- -------\n");

	printf(" ------ env ------\n");

	for (i = 0; env[i] != NULL; i++) {
		printf("env[%d]: %s\n",i,  env[i]);
	}
	printf(" ------- ---- -------\n");


	return 0;
}

c 语言里 0, ‘\0’, NULL 都是0

结构体

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>



//1
struct teacher
{
	int id;
	char name[64];
};

struct teacher t5 = { 5, "laoshi5" };

//2
struct {
	int id;
	char name[64];
} t3, t4;//匿名的结构体类型  类型只能定义一次, 不能通过函数传参


//3
typedef struct  _teacher
{
	int id;
	char name[64];
} teacher_t;  //最常用的写法

/*
struct _teacher
{
	int id;
	char name[64];
};

typedef struct _teacher teacher_t;
*/




void print_teacher(struct teacher* p1)
{
	printf("id = %d\n", p1->id);
	printf("name = %s\n", p1->name);
}

void print_teacher2(struct teacher t) //t = t1     int a = b; struct teacher t1 = t2
{
	printf("===== print_teacher2===\n");
	printf("id = %d\n", t.id);
	printf("name = %d\n", t.name);
}

void copy(struct teacher to, struct teacher from)
{
	to = from;
}

void copy2(struct teacher *to, struct teacher *from)
{
	*to = *from;
}

/*
void print_teacher2(struct {
	int id;
	char name[64];
})
*/
 
int main(void)
{
	struct teacher  t1;
	struct teacher  t8;
	teacher_t t6 = {6, "laoshi6"};
	//teacher t7;  // C语言中  定义一个结构体 必须加上struct 关键字  C++不用加

	t1.id = 10;
	strcpy(t1.name, "laoshi1"); // . 是寻址操作符 name 相对于t1 的偏移量,没有操作内存


	// 通过指针方式操作内存空间
	{
		teacher_t *p = NULL;
		p = &t6;
		p->id;
		p->name;
	}

	print_teacher(&t1);
	print_teacher(&t5);

	print_teacher2(t1);  //

	printf("=====\n");
	copy2(&t8, &t1);
	print_teacher(&t8);

	struct teacher t9 = t1; //int a = b;


	return 0;
}

结构体作函数参数

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct teacher
{
	int id;
	char name[64];
};

//开辟 struct teacher 的空间
void create_teachers(struct teacher **tpp, int num)
{
	struct teacher *p = NULL;

	if (tpp == NULL) {
		return;
	}

	p = (struct teacher *)malloc(sizeof(struct teacher) *num);
	if (p == NULL) {
		return;
	}

	*tpp = p;

	return;
}

void print_teachers(struct teacher *tp, int num)
{
	int i = 0;
	printf("=======\n");
	for (i = 0; i < num; i++) {
		printf("id :%d\n", tp[i].id);
		printf("name : %s\n", tp[i].name);
	}
}

void free_teachers(struct teacher **tpp)
{
	if (tpp == NULL) {
		return;
	}

	struct teacher *tp = *tpp;

	if (tp != NULL) {
		free(tp);
		*tpp = NULL;
	}

	return;
}

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


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

	print_teachers(tp, num);

	free_teachers(&tp);


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




	return 0;
}

结构体嵌套二级指针

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值