一.结构体的定义与使用
结构体格式
struct 结构体名称
{
//结构体成员列表
char name[100];
int age;
int score;
};
也可以直接在}后定义结构体变量,此时为全局变量
如 }stu;
int main()
{
//创建结构体变量(结构体类型,结构体变量)
struct student stu;//struct student是结构体的一个类型(如int char);stu是结构体变量
stu.name=张三;//错误!name为一个数组名,数组名不允许被赋值!strcpy(stu.name,“张三”);//正确!
stu.age=18;
stu.score=100;或直接赋值
struct student stu={“张三”,18,100};
}
输入结构体的值
int main()
{
struct student stu;
scanf("%s%d%s",stu.name,&stu.age,&stu.score)
}
二.结构体数组
结构体数组的定义与使用
#include<stdio.h>
struct stduent//结构体的声明
{
char name[100];
int age;
int score;
}; //注意分号不能少,这也相当于一个语句
int main()
{
struct stduent stu[3]=//对结构体进行初始化 (定义)
{
{"张三",18,100},
{"李四",22,99},
{"王五",19,88},
};
//输出
for(int i=0;i<3;i++)
{
printf("姓名:%s\n",stu[i].name);//访问结构体变量元素
printf("年龄:%d\n",stu[i].age);//访问结构体变量元素
printf("分数:%d\n",stu[i].score);//访问结构体变量元素
printf("\n");
}
return 0;
}
开辟堆空间存储结构体
#include<stdlib>
struct student *p=(struct student)malloc(sizeof(struct student)*10);
//为了简便输入
typedef struct student ss;//将结构体起别名为ss
//则语句可改成
ss *p=(ss)malloc(sizeof(ss)*10);
三.结构体嵌套结构体
#include<stdio.h>
typedef struct student ss;//将结构体起别名为ss
struct scores //分数
{
int c1;
int cpp;
int cs;
};
struct stduent //学生信息
{
char name[21];
int age;
struct scores ss;//定义结构体
char addr[51];
};
int main()
{
ss stu={"张三",18,99,99,99,"广东"};//struct student stu={"张三",18,99,99,99,"广东"};
//或
ss stu;//struct student stu;
strcpy(stu.name,"张三");
stu.age=18;
stu.ss.c1=99;
stu.ss.cpp=99;
stu.ss.cs=99;
printf("%s\n%d\n%d\n%d\n%d\n%s\n",stu.name,stu.ss.c1,stu.ss.cpp,stu.ss.cs,stu.addr);
}
四.结构体与指针
结构体成员为指针类型
//结构体成员为指针类型
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
char*name;
int age;
int*scores;
char*addr;
};
int main()
{
struct student stu;
// stu.name="张三";"张三"为常量字符!不可修改
stu.name=(char*)malloc(sizeof(char)*10);
stu.age=(int)malloc(sizeof(int)*10);
stu.scores=(int*)malloc(sizeof(int)*10); //指针定义时需要开辟空间!!!
stu.addr=(char*)malloc(sizeof(char)*20);
strcpy(stu.name,"张三");
stu.age=18;
stu.scores[0]=99;
stu.scores[1]=99;
stu.scores[2]=99;
strcpy(stu.addr,"广东省广州市天河区");
printf("%s\n%d\n%d\n%d\n%d\n%s\n",stu.name,stu.age,stu.scores[0],stu.scores[1],stu.scores[2],stu.addr);
free(stu.name);
free(stu.age); //内存必须逐步释放
free(stu.scores);
free(stu.addr);
return 0;
}
结构体为指针
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student ss;
struct student
{
char*name;
int age;
int*scores;
char*addr;
};
int main()
{
//开辟结构体大小的空间
ss*p=(ss*)malloc(sizeof(ss)*3);
/*
直接赋值
ss stu={"武松",19,99,99,99,广东};
ss *p=&stu;
*/
for(int i=0;i<3;i++)//开辟内层空间
{
p[i].name=(char*)malloc(sizeof(char*)*21);
p[i].scores=(int*)malloc(sizeof(int*)*21);
p[i].addr=(char*)malloc(sizeof(char*)*51);
}
//开辟内层空间
for(int i=0;i<3;i++)
{
scanf("%s%d%d%d%d%s",p[i].name,&p[i].age,&p[i].scores[0],&p[i].scores[1],&p[i].scores[2],p[i].addr);
}
//打印
for(int i=0;i<3;i++)
{
printf("姓名:%s\n",(p+i)->name);
printf("年龄:%d\n",(p+i)->age);
printf("成绩1:%d\n",(p+i)->scores[0]); //(p+i)->与 p[i].都可以!
printf("成绩2:%d\n",(p+i)->scores[1]);
printf("成绩3:%d\n",p[i].scores[2]);
printf("地址:%s\n",p[i].addr);
printf("\n");
}
//释放内存空间
for(int i=0;i<3;i++)
{
free(p[i].name);
free(p[i].scores);
free(p[i].addr);
}
return 0;
}
五.结构体与函数(作为函数参数)
一般结构体与函数
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student ss;
struct student
{
char name[21];
int age;
int scores;
char addr[51];
};
void fun(ss stu1)
{
strcpy(stu1.name,"李四");
printf("%s\n",stu1.name);
}
int main()//值传递
{
ss stu={"张三",18,99,"广州"};
fun(stu);
printf("%s\n",stu.name);
/*输出结果为李四 张三
值传递时形参不会改变实参的值,因此在主函数中还是张三
*/
return 0;
}
结构体指针作为函数参数传递
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student ss;
struct student
{
char name[21];
int age;
int scores[3];
char addr[51];
};
void fun(ss*p)//传递的地址用指针接受
{
strcpy(p->name,"李四");
printf("%s\n",p->name);
}
int main()//地址传递(结构体指针作为函数参数传递)
{
ss stu={"张三",18,99,"广州"};
fun(&stu);//传递地址,用指针接受
printf("%s\n",stu.name);
return 0;
}
结构体数组作为函数参数传递
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student ss;
struct student//结构体的声明要放在最前面!
{
char name[21];
int age;
int scores[3];
char addr[51];
};
void bubblesort(ss* stu, int len)
//结构体数组作为函数参数传递时会退化为指针丢失精度,因此需要传递个数过来
{
for (int i = 0; i < len - 1; i++)
for (int j = 0; j < len - i - 1; j++)
{
if ((stu + j)->age > (stu + j + 1)->age)
{
//交换结构体变量
ss temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
int main()
{
ss stu[3] =
{
{"张三",30,100,100,100,"广州"},
{"李四",33,90,90,990,"深圳"},
{"王五",38,80,80,80,"东莞"},
};
bubblesort(stu, 3);
for (int i = 0; i < 3; i++)
{
printf("%s\n", (stu + i)->name);
printf("%d\n", (stu + i)->age);
printf("%d\n", (stu + i)->scores[0]);
printf("%d\n", (stu + i)->scores[1]);
printf("%d\n", (stu + i)->scores[2]);
printf("%s\n", (stu + i)->addr);
printf("\n");
}
return 0;
}
文章转自布尔博客
欢迎关注技术公众号,获取更多硬件学习干货!
我们能为你提供什么?
技术辅导:C++、Java、嵌入式软件/硬件
项目辅导:软件/硬件项目、大厂实训项目
就业辅导:就业全流程辅导、技术创业支持
对接企业HR:培养输送优质性人才