对于单片机C里面结构体的认识

来源:互联网 发布:欧文2017赛季数据 编辑:程序博客网 时间:2024/06/06 13:57
最近在学习单片机,作为初学者,虽然在大学的时候学了点C语言但是因为后面学了java所以C语言的基础又还给了老师,今天在看STM8 的教程的时候看到一段对结构体的描述,记在这里做个备忘。
typedef struct GPIO_struct{  __IO uint8_t ODR; /*!< Output Data Register */  __IO uint8_t IDR; /*!< Input Data Register */  __IO uint8_t DDR; /*!< Data Direction Register */  __IO uint8_t CR1; /*!< Configuration Register 1 */  __IO uint8_t CR2; /*!< Configuration Register 2 */}GPIO_TypeDef;

这就是库函数里面对GPIO的结构体定义。对于理解可以用

typedef struct Man{    int age;    int score;}man;int main(){    Man man1 = {20,80};    Man man2 = {30,70};    Man man3 = {40,99};    printf("%d\n",man1.age);    printf("%d\n",man3.score);}

这样理解更加容易理解 要一个人
怎么办呢 先typedef struct 一个名字叫做Man的结构体
定义内容,
年龄 分数 等等
然后结束带个变量名man
其实声明的结构体名 和结束的变量名可以相同。

typedef struct man{    int age;    int score;}man;  //还叫manint main(){    man man = {40,50};//还叫man    printf("%d\t%d\n",man.age,man.score);}

这样也可以,就是可读性差了点。
我以后还是会结构体名字和变量名不一样,免得自己混淆了。
关于更深入的用指针取结构体地址的,单片机貌似用不到。暂时先搁置,以后用到再慢慢了解~

0 0