返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
数据结构

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

曦远 (DealiAxy)约 5 分钟
目录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:

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);
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 许可协议。转载请注明出处!

相关文章

Android开发:对Gradle的折腾记录

前言 有点烦,最近给电脑升级一下配置,加了内存和固态硬盘,结果新内存不太兼容,导致电脑频繁死机,去找商家换了一条内存之后就好了,这也导致我贸然重装了系统 = =... 有一点资料放在系统盘忘记保存结果丢了。今天下午才把新系统配置好,所以这两天都没有办法写推文。 (不得不说Chrome真是好用,这同步功能免去了很多麻烦。) 首先喷一下Gradle 缺点:占用资源大,速度又慢,在国内用网络还很差。 今

2022-05-27 · Android开发 · 约 1 分钟

C#学习(二)匿名方法和委托的多种使用方式

前言 上次简单介绍了委托的概念和使用,今天继续,并且引入匿名方法的概念,匿名方法在很多语言里面都有,比如Python、js、java8,尤其js,前端小伙伴可能深受js回调地狱之苦,这里面匿名方法功不可没。 js的匿名方法就像这样: 这还没用lambda,因为我想下篇再介绍lambda。 document.querySelector('.btn').onclick = function () {

2021-12-18 · C# · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…