C语言之结构体基础
结构体基础
结构体定义
struct 结构体名 //结构体名
{
//数据类型定义变量
//嵌套形式
}; //分号一定不能少
结构体作用
- 用来抽象一类事物共同属性(特征)
- 特征通常用数据类型描述
人类:姓名,年龄,身高
struct MAN
{
char name[20];
int age;
int x;
};
各式各样创建结构体和结构体变量的方式
- 最常规的创建方式
struct MAN
{
char name[20];
int age;
};
-
缺省的写法
- 只能有一个结构体变量
struct { char name[20]; int age; }AB; //gg就是该结构体唯一的一个结构体变量 -
用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}; 错误写法
结构体数据访问
- 结构体中的数据只能用结构体变量访问
- 结构体变量:普通结构体变量、结构体指针
- 结构体变量访问方式
- 结构体变量.成员
- 结构体指针访问方式
- 结构体指针->成员
- 结构体中的字符数组必须采用拷贝的方式访问
- 结构体中的数组必须采用循环的方式访问
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;
结构体变量的使用
-
普通结构体变量
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); } -
结构体变量充当返回值
struct Pos
{
int i;
int j;
};
//推箱子 查找人物的位置
struct Pos searchRole()
{
struct Pos pos;
//数组的查找
pos.i = 10;
pos.j = 20;
return pos;
}
结构体指针
-
指针指向变量
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); -
堆内存申请指针
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; -
指针表示变量
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;
}
本文来自博客园,作者:{oy},转载请注明原文链接:https://www.cnblogs.com/Oysen/p/17005607.html

浙公网安备 33010602011771号