【学习生涯】结构体数组

C语言结构体嵌套

用老师的话来说就是剥洋葱(比较简单)

  • 变量方式访问: .
  • 指针的方式访问:->
  • 或者直接包含

结构体数组定义

//和基本数据类型定义方式是一样的
#include <stdio.h>
#include <stdlib.h>
struct MM 
{
	char name[20];
	int age;
}mmArray[3];
struct MM g_array[3];

int main() 
{
	struct MM array[3];
	return 0;
}

结构体数组初始化

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct MM 
{
	char name[20];
	int age;
}mmArray[3]= { {"小芳",18},{"小美",28},{"小花",27} };
struct MM g_array[3]= { {"小芳",18},{"小美",28},{"小花",27} };
int main() 
{
	//创建时候做初始化
	struct MM array[3] = { {"小芳",18},{"小美",28},{"小花",27}};
	memset(array, 0, sizeof(struct MM) * 3);
	//循环初始化
	struct MM temp[3]; 
	for (int i = 0; i < 3; i++) 
	{
		scanf_s("%s%d", temp[i].name,20, &temp[i].age);
	}
	return 0;
}

结构体遍历

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct MM 
{
	char name[20];
	int age;
}mmArray[3]= { {"小芳",18},{"小美",28},{"小花",27} };
struct MM g_array[3]= { {"小芳",18},{"小美",28},{"小花",27} };
int main() 
{
	//小芳 18
	//创建时候做初始化
	struct MM array[3] = { {"小芳",18},{"小美",28},{"小花",27}};
	memset(array, 0, sizeof(struct MM) * 3);
	//循环初始化
	struct MM temp[3]; 
	for (int i = 0; i < 3; i++) 
	{
		scanf_s("%s%d", temp[i].name,20, &temp[i].age);
	}
	for (int i = 0; i < 3; i++) 
	{
		printf("%s\t%d\n", temp[i].name, temp[i].age);
	}
	for (int i = 0; i < 3; i++)
	{
		printf("%s\t%d\n", (temp+i)->name, (temp + i)->age);
	}
	return 0;
}

结构体指针基本操作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct MM 
{
	char name[20];
	int age;
};
void testFirst() 
{
	struct MM mm = { "小美",19 };
	struct MM* pmm = NULL;
	pmm = &mm;
	printf("%s\t%d\n", pmm->name, pmm->age);

	struct MM array[3] = { {"小美",19},{"小美2",28},{"小美3",23} };
	pmm = array;
	for (int i = 0; i < 3; i++) 
	{
		printf("%s\t%d\n", pmm[i].name, pmm[i].age);
	}
}
int main() 
{
	testFirst();
	return 0;
}

结构体指针动态申请内存

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct MM 
{
	char name[20];
	int age;
};
void testSecond() 
{
	//申请一个变量内存
	struct MM* pmm = (struct MM*)malloc(sizeof(struct MM));
	assert(pmm);
	strcpy_s(pmm->name, 20, "小芳");
	pmm->age = 19;
	printf("%s\t%d\n", pmm->name, pmm->age);
	free(pmm);
	pmm = NULL;
	//申请一段内存
	struct MM* pArray = (struct MM*)malloc(sizeof(struct MM) * 3);
	assert(pArray);
	for (int i = 0; i < 3; i++) 
	{
		strcpy_s(pArray[i].name,20, "美美");
		pArray[i].age = 19;
		printf("%s\t%d\n", (pArray+i)->name, (pArray+i)->age);
	}
	free(pArray);
	pArray = NULL;
}
int main() 
{
	testSecond();
	return 0;
}

结构体嵌套结构体指针

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct MM 
{
	char name[20];
	int age;
};
struct Info 
{
	int num;
	struct MM* pmm;
};
void testThird() 
{
	struct Info info = { 1001,NULL};
	struct MM mm = { "小妹",18 };
	struct Info info2 = { 1002,&mm };

	struct Info* pinfo = (struct Info*)malloc(sizeof(struct Info));
	assert(pinfo);

	pinfo->num = 1004;
	pinfo->pmm = (struct MM*)malloc(sizeof(struct MM));
	assert(pinfo->pmm);
	strcpy_s(pinfo->pmm->name, 20, "小小");
	pinfo->pmm->age = 29;

	printf("%d\t%s\t%d\n", pinfo->num, pinfo->pmm->name, pinfo->pmm->age);
}
int main() 
{
	testFirst();
	testSecond();
	testThird();
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值