STM32单片机C语言编程结构体相关知识

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
} student1,student2;
(3)初始化
struct Student
{
int name;
} student1={99};
Printf(“%d”,student.score);
2、结构体数组
struct Person
{
Char name[20];
Int count;
}leader[3]={“Li”,1 ,”Zhang”,2 ,“Wang”,3};
STM32
一、基础
(1)各个变量需要在内存地址上有联系,且各个变量可以是不同类型(优于数组)
(2)定义结构体类型:
struct Student
{
int score;
};
定义某个结构体类型的变量:
struct Student student1,student2;
或者
struct Student
{
int name;
int main()
{struct student *p; //定义指向struct student结构体的数组
for (p=stu;p<stu+3;p++)
printf("%5d %-20s %2c %4d\n",p->num, p->name, p->sex, p->age);
return 0;
}
P++:p所增加的值为struct Student结构体数组一个元素所占的字节数。本例一个元素理论上占字节为:4+20+1+4=29字节,实际分配32字节。
5、指向结构体数组的指针
#include <stdio.h>
struct student
{int num;
char name[20];
char sex;
int age;
};
struct student stu[3]={{10101,"Li Lin",'M',18},{10102,"Zhang Fun",'M',19},{10104,"Wang Min",'F',20}};
{
Int score;
} ;
structStudentstu; //定义结构体
struct Student*p; //定义指向struct Student类型的结构体指针
p=&stu; // p指向stu
stu.score=100;
printf(“%d\n”,(*p).score); //或p->score
3、typedef用法
(ቤተ መጻሕፍቲ ባይዱ)定义新的类型名
如typedof int Integer; //指定用Integer为类型名,作用与int相同。
int a;与integer a;等价
(2)在结构体的应用
typedef struct
{
int month;
int day;
int year;
} Date;
定义了一个新的类型名Date,代表上面的一个结构体类型。可以用新的类型名Date去定义变量:
Date birthday;//定义结构体变量birthday
Date *p;//定义结构体指针变量p,指向此结构体类型数据
4、结构体指针
指向结构体的指针,一个结构体变量的起始地址就是这个结构体变量的指针。如果把一个结构体变量的起始地址存放在一个指针变量中,那么这个指针变量就指向该结构体。
struct Student
相关文档
最新文档