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 许可协议。转载请注明出处!
相关文章
StarBlog开源博客项目 - 2023年底更新内容一览
前台UI勉强能看,后台总算不那么辣眼睛了。数据库分页改对了,不然性能差得离谱。新增slug检查?哦,终于知道避免重复了。
2023-12-16 · StarBlog博客开发笔记 · 约 1 分钟
开始开发一个新博客了,复古风Metro风格,顺便diss一下js
emmm 我原本的博客是用wordpress搭建的,当年七牛云还是免费时用它做的CDN,但是现在收费了,如果维持目前的博客流量的话每个月都要支付不少费用,于是只好把七牛云给停了= =,这也导致我博客里面很多图片都访问不了,页面布局也出问题了,不想折腾了,而且wordpress用起来总觉得不顺手,还是自己做一个吧~ 计划 最近刚好在学SpringBoot啥的,本来想用springboot搭建一个博客
2022-07-18 · Web前端 · 约 1 分钟
谁能拒绝一台能装进口袋的电脑?GPD Win Mini 开箱体验
啧,又是个满足童年幻想的玩具。GPD Win Mini确实能塞进口袋,但性能释放也就那样,烤机时风扇吵得让人皱眉。接口倒是意外地齐全,不过这种小玩意也就图个新鲜,真当生产力工具?想多了。
2025-10-28 · 掌机 · 约 1 分钟
又买了一个 Thinkbook16+ 的原装245w充电器
Thinkbook16+对PD挑食得离谱,140W头也只喂到60W,一开GPU还偷电池,只好再买原装245W。京东客服连链接都能推错,拼多多不到200拿下,谁不专业一目了然。黑色款纯为和办公室白色区分——不想背电源通勤,就这么简单。
2026-11-12 · 充电器 · 约 1 分钟
讨论
共 0 条加载评论中…