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 许可协议。转载请注明出处!
相关文章
第一次体验开放式耳机,漫步者XClip开箱
十来个TWS最后都沦为电池消耗品,这次两百块买个耳夹式试水。音质确实超预期,低频却轰得人头疼;戴两小时照样不适,戴上去还没半入耳利索。至于近200MB的国产App,臃肿得毫不意外。
2026-10-26 · 音频 · 约 1 分钟
Android开发:对Gradle的折腾记录
前言 有点烦,最近给电脑升级一下配置,加了内存和固态硬盘,结果新内存不太兼容,导致电脑频繁死机,去找商家换了一条内存之后就好了,这也导致我贸然重装了系统 = =... 有一点资料放在系统盘忘记保存结果丢了。今天下午才把新系统配置好,所以这两天都没有办法写推文。 (不得不说Chrome真是好用,这同步功能免去了很多麻烦。) 首先喷一下Gradle 缺点:占用资源大,速度又慢,在国内用网络还很差。 今
2022-05-27 · Android开发 · 约 1 分钟
AspNetCore开发笔记:WebApi项目集成企业微信和公众号
微信开发文档烂得离谱,直接用SKIT库搞定。异步处理消息,图片下载内存处理,公众号被动回复5秒内搞定。啧啧。
2025-12-22 · Asp-Net-Core开发笔记 · 约 1 分钟
C#学习(二)匿名方法和委托的多种使用方式
前言 上次简单介绍了委托的概念和使用,今天继续,并且引入匿名方法的概念,匿名方法在很多语言里面都有,比如Python、js、java8,尤其js,前端小伙伴可能深受js回调地狱之苦,这里面匿名方法功不可没。 js的匿名方法就像这样: 这还没用lambda,因为我想下篇再介绍lambda。 document.querySelector('.btn').onclick = function () {
2021-12-18 · C# · 约 1 分钟
讨论
共 0 条加载评论中…