C Language Prerequisites: typedef and typedef struct
typedef typedef existing new; The function of typedef is to create a new data type name, for example: typedef int Length; // data type typedef char *String; // character pointer typedef struct tnode *Treeptr; // pointer typedef int (*PFI) (char *, char *); // pointing to fun
目录6 个章节
typedef
typedef existing new;
The function of typedef is to create a new data type name, for example:
typedef int Length; // 数据类型
typedef char *String; // 字符指针
typedef struct tnode *Treeptr; // 指针
typedef int (*PFI) (char *, char *); // 指向函数的指针
Length l = 120;
printf("%d \n", l);
A typedef declaration does not create a new type; it merely adds a new name for an existing type.
Two key benefits of using typedef are:
- It can parameterize the program, improving the program's portability.
- It can provide better descriptiveness for the program, increasing readability.
struct
struct is used to define a new data type that contains multiple members. There are the following 4 ways to define it:
Method 1: Do not specify a type name, declare the struct variable directly (not recommended)
struct {
int age;
} student;
student.age = 18;
printf("%d \n", student.age);
Method 2: First define the type name, then declare the struct variable
Characteristic: You need to use the struct type name every time to declare a variable.
struct Student {
char name;
int age;
};
struct Student student1;
student1.age = 20;
Method 3: Define the type name and declare the struct variable
struct HeadTeacher {
int age;
} h1;
h1.age = 50;
struct HeadTeacher h2;
h2.age = 70;
printf("head teacher1 age = %d \n", h1.age);
printf("head teacher2 age = %d \n", h2.age);
Method 4: Define the type name and give it an alias (recommended)
typedef struct {
int age;
} Teacher;
Teacher t;
t.age = 30;
printf("teacher age = %d \n", t.age);
_EOF_
C Language Prerequisites: typedef and typedef struct
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
基于.NetCore开发博客项目 StarBlog - (32) 第一期完结
本文记录一下 StarBlog 项目的当前状态与接下来 v2 版本的开发规划。
2024-12-02 · StarBlog博客开发笔记 · 约 1 分钟
ChatGLM-6B本地部署
下载项目代码 git clone https://github.com/THUDM/ChatGLM-6B.git 创建虚拟环境,建议使用 conda 管理 conda create -n chatglm python==3.8 安装依赖 conda activate chatglm pip install -r requirements.txt conda install cudatoolkit=
2026-05-09 · LLM · 约 1 分钟
操作系统学习三:进程调度与死锁-以及银行家算法避免死锁--NetCore实现
前言 这是操作系统学习的第三篇啦,关于进程调度有很多内容,操作系统在调度进程的时候最容易遇到的问题就是死锁了,银行家算法是一个典型的避免死锁算法。 死锁的概念 先来了解一下死锁的基本概念:一组竞争系统资源或相互通信的进程相互的“永久”阻塞。若无外力作用,这组进程将永远不能继续执行。 看下面两幅图片,左边是可能产生死锁的状态,四辆汽车(进程)要竞争同一个资源(通过路口),如果系统调度不当,就会陷入死
2021-12-18 · 计科基础 · 约 1 分钟
C#学习(三)深入理解委托、匿名方法和-Lambda-表达式
前言 lambda的概念建立在委托的基础上,委托,实现了类型安全的回调方法。在.NET 中回调无处不在,所以委托也无处不在,事件模型建立在委托机制上,Lambda 表达式本质上就是一种匿名委托。本文中将完成一次关于委托的旅行,全面阐述委托及其核心话题,逐一梳理委托、委托链、事件、匿名方法和 Lambda 表达式。 委托的定义 了解委托,从其定义开始,通常一个委托被声明为: public deleg
2021-12-18 · C# · 约 1 分钟
讨论
共 0 条加载评论中…