C语言之结构体基础

结构体基础

结构体定义

struct 结构体名				//结构体名
{
   //数据类型定义变量
   //嵌套形式
};			//分号一定不能少

结构体作用

  • 用来抽象一类事物共同属性(特征)
  • 特征通常用数据类型描述

人类:姓名,年龄,身高

struct MAN
{
	char name[20];
    int age;
    int x;
};

各式各样创建结构体和结构体变量的方式

  1. 最常规的创建方式
struct MAN 
{
	char name[20];
	int age;
};

  1. 缺省的写法

    • 只能有一个结构体变量
    struct 
    {
    	char name[20];
    	int age;
    }AB;       //gg就是该结构体唯一的一个结构体变量 
    
  2. 用typedef创建结构体

    typedef struct Node 
    {
    	int data;
    }NODE,*LPNODE,ARRAY[3];
    //定义结构体的时候,一旦有typedef ,结构体后面的全部都是别名
    //直接不看中间
    //typedef struct Node	NODE;   结构体变量的别名
    //typedef struct Node* LPNODE;	结构体指针别名
    //typedef struct Node ARRAY[3];	结构体数组的别名
    struct Date 
    {
    	int year;
    	int month;
    	int day;
    

创建结构体变量

//类型 变量名
struct MAB
{
	int age;    
}bs;
struct FGH mba1;
int main()
{
	struct FGH mba2;
    return 0;
}
  • 结构体变量初始化

    • 数据一定要带大括号
    • 一定要按照结构体定义的时候,数据的类型顺序一致
    • 不能先创建,后初始化
    struct Data
    {
    	char first[20];
        int second;
        double third;
    };
    struct Data data={"first",2,3.3f};
    //struct Data data2;
    //data2={"first",2,3.3f};    错误写法
    

结构体数据访问

  1. 结构体中的数据只能用结构体变量访问
    • 结构体变量:普通结构体变量、结构体指针
  2. 结构体变量访问方式
    • 结构体变量.成员
  3. 结构体指针访问方式
    • 结构体指针->成员
  4. 结构体中的字符数组必须采用拷贝的方式访问
  5. 结构体中的数组必须采用循环的方式访问
struct Data data2;
//结构体中字符数组必须采用拷贝
strcpy_s(data2.first,20, "手动初始化");
data2.third = 3;
data2.second = 2;
	
//打印变量中元素,以前用什么格式现在还是用格式
printf("%s\t%d\t%.2lf\n", data2.first, data2.second, data2.third);
struct Data* pStu = &data2;
pStu->second = 22;
pStu->third = 33;
printf("%s\t%d\t%.2lf\n", pStu->first, pStu->second, pStu->third);
printf("%s\t%d\t%.2lf\n",(*pStu).first, (*pStu).second, (*pStu).third);
(*pStu).second = 123;

结构体变量的使用

  1. 普通结构体变量

    struct MAN 
    {
    	char name[20];
    	int age;
    	int num;
    };
    void modify(int a) 
    {
    	a = 100;
    }
    void initMAN(struct MAN* pMAN) 
    {
    	printf("请输入美女的信息(name,age,num):");
    	scanf_s("%s%d%d", pMAN->name,20, &pMAN->age, &pMAN->num);
    }
    void printMAN(struct MAN man)
    {
    	printf("姓名\t年龄\t编号\n");
    	printf("%s\t%d\t%d\n", man.name, man.age, man.num);
    }
    
  2. 结构体变量充当返回值

struct Pos
{
	int i;
	int j;
};
//推箱子 查找人物的位置
struct Pos searchRole() 
{
	struct Pos pos;
	//数组的查找
	pos.i = 10;
	pos.j = 20;
	return pos;
}

结构体指针

  1. 指针指向变量

    struct MAN 
    {
    	char name[20];
    	int age;
    };
    
    	struct MAN mm = { "man",18 };
    	struct MAN* pMAN = &man;
    	printf("%s\t%d\n", pMAN->name, pMAN->age);
    
  2. 堆内存申请指针

    struct MAN* pMmeory = (struct MAN*)malloc(sizeof(struct MAN));
    strcpy_s(pMmeory->name,20, "人类");
    	pMmeory->age = 25;
    	printf("%s\t%d\n", pMmeory->name, pMmeory->age);
    	free(pMmeory);
    	pMmeory = NULL;
    
  3. 指针表示变量

    struct MAN* createMMData(const char* name, int age)
    {
    	struct MAN* pREN = (struct MAN*)malloc(sizeof(struct MAN));
    	assert(pREN);
    	strcpy_s(pREN->name, 20, name); //strcpy
    	pREN->age = age;
    	return pREN;
    }
    
    struct MAN* p = createMMData("小可爱", 29);
    	printf("%s\t%d\n", p->name, p->age);
    	free(p);
    	p = NULL;
    

结构体数组

#include <stdio.h>
#include <string.h>
#include <assert.h>
struct Student 
{
	char name[20];
	int age;
	int num;
	int score[3];
};
void printStudentInfo(struct Student stu[],int arrayNum)
{
	printf("姓名\t年龄\t编号\t数学\t英语\t体育\n");
	for (int i = 0; i < arrayNum; i++) 
	{
		printf("%s\t%d\t%d\t", stu[i].name, (stu + i)->age, stu[i].num);
		//整数数组必须用循环
		for (int j = 0; j < 3; j++) 
		{
			printf("%d\t", stu[i].score[j]);
		}
		printf("\n");
	}
}
int main() 
{
	struct Student stu[3] = {
		{"张三",18,1001,{89,89,89}},
		{"李四",20,1002,{90,90,90}},
		{"王五",22,1003,{78,78,89}}
	};
	//stu[0] stu[1] stu[2]
	printStudentInfo(stu, 3);

	struct Student stuSystem[100];
	int curSize = 0;

	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不想写代码的懒大王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值