title: typedef总结
date: 2024-10-18 10:00:00
tags: [linux,typedef]
categories: c

typedef总结

C/C++ typedef用法详解(真的很详细)-CSDN博客

以下是3中常见的通过typedef定义重命名结构体的形式:

匿名方式定义结构体

1
2
3
4
5
6
typedef struct {
int a;
char b;
} mystruct;
//使用
mystruct s1;

命名方式定义结构体(用的最多)

1
2
3
4
5
6
typedef struct mystruct {
int a;
char b;
} T_mystruct;
//使用
T_mystruct s1;

T_mystruct为struct mystruct 定义了一个类型别名

定义了一个结构体:mystruct.可以通过struct mystruct xxx;来定义结构体

更推荐的规范做法(实际上感觉用的很少):

1
2
3
4
5
6
7
struct mystruct
{
int a;
int b;
};
typedef struct mystruct T_mystruct;
//typedef struct mystruct pT_mystruct;
  • 这里定义了一个名为 mystruct 的结构体,包含两个整数成员 ab
  • 使用 typedefstruct mystruct 定义了一个别名 T_mystruct
  • T_mystruct 可以用来声明 mystruct 类型的变量