C语言:typedef关键字

本文详细介绍了C语言中typedef关键字的作用,特别是在单片机开发中的应用,如简化寄存器类型定义。同时,讨论了typedef与结构体结合使用的方式,包括普通重命名、去除结构体名以及指针类型的定义。举例展示了如何使用typedef定义结构体及结构体指针,并在实际代码中应用这些定义。通过typedef,可以提高代码的可读性和一致性。
摘要由CSDN通过智能技术生成

目录

1.typedef关键字干嘛的

2.在单片机开发当中使用typedef比较常见,寄存器8位,16位,32位

3. 和结构体结合命名:

       3.1普通重命名。

       3.2重命名的另一种,把结构体本身给的名字Student的去掉。 

       3.3普通重命名后附带命名一个指针(最常见)。

       3.3结构体里的函数指针,不能使用结构体括号外的名字

4. typedef关键字和结构体的使用,代码如下:


1.typedef关键字干嘛的

  •     typedef为C语言的关键字
  •     作用是为一种数据类型定义一个新名字。
  •     这里的数据类型包括内部数据类型(int,char等)和自定义数据类型(struct等)

 

  •     和struct来匹配是为了代码编写简洁
  •     和普通类型匹配,通过名字来获取一些信息

2.在单片机开发当中使用typedef比较常见,寄存器8位,16位,32位

  •        在寄存器中比如一个char型,其实是可以在寄存器8位,16位,32位中重合的,都可以有其表示的方式。
  •       比如数据在寄存器8位已经足够,你配了一个32位的寄存器给他,就会造成浪费。为了给人有好的阅读,所以用了typedef重命名。

typedef unsigned char         u_int8;    //  无符号的字符型   此时就是将unsigned char这一串命名为u_int8,对应寄存器8位。
       

typedef unsigned short int   u_int16;   //无符号的短整型   此时就是将unsigned short int 这一串命名为u_int16
       

typedef unsigned int            u_int32;

3. 和结构体结合命名:

  3.1普通重命名。

typedef struct Student 
{
	int score;
    char *name;
    void (*p)(struct Student stu);  //void (*p)(STU stu); 不可以换成这样,因为STU是在结构体的定义大括号之后,先有大括号里的才开始外面。
}STU;


此时:struct Student 就相当于 STU

       3.2重命名的另一种,把结构体本身给的名字Student的去掉。 

typedef struct
{
	int score;
    char *name;  
}STU;

       3.3普通重命名后附带命名一个指针(最常见)。

typedef struct Student 
{
	int score;
    char *name;
    void (*p)(struct Student stu);  //void (*p)(STU stu); 不可以换成这样。因为STU是在结构体的定义大括号之后,先有大括号里的才开始外面。
}STU *PSTU;


STU   就相当于 struct Student 
*PSTU 就相当于 struct Student*   是一个指针

        3.3结构体里的函数指针,不能使用结构体括号外的名字

                     比如上面的代码中的:void (*p)(struct Student stu);  

                     不可以写成:                void (*p)(STU stu); 

                     因为STU是在结构体的定义大括号之后,先有大括号里的才开始外面。

4. typedef关键字和结构体的使用,代码如下:

#include <stdio.h>
#include <stdlib.h>

typedef unsigned char u_int8;    
typedef unsigned short int u_int16;      
typedef unsigned int u_int32;

typedef struct Student  
{
	int score;
    char *name;
    void (*p)(struct Student stu);  //void (*p)(STU stu); 不可以。因为STU是在结构体的定义大括号之后
}STU,*PSTU;

int main()
{
    //单片机开发中常见
	u_int8 data = 10;
    u_int16 data2 = 20;
    u_int32 data3 = 40;
    printf("%d,%d,%d\n",data,data2,data3);
    

	//未加指针变量,只有STU时   struct Student stu1;
    STU stu1;                             
    stu1.score = 100;
    printf("Score=%d\n",stu1.score);
    
	//typedef之后的结构体用法。在结构体那里加了指针变量*PSTU后   struct Student* pstu
    PSTU pstu;                           
    pstu = (PSTU)malloc(sizeof(STU));
    pstu->score = 99;
    printf("Score2=%d\n",pstu->score);	
  
 
    //旧结构体用法
	struct Student* pstu3;               
    pstu3 = (struct Student*)malloc(sizeof(struct Student));
    pstu3->score = 99;
    printf("Score3=%d\n",pstu3->score);	
 	    
	system("pause");
	return 0;
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

枕上

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

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

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

打赏作者

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

抵扣说明:

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

余额充值