返回文章列表文章索引阅读全文
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 许可协议。转载请注明出处!

相关文章

开始开发一个新博客了,复古风Metro风格,顺便diss一下js

emmm 我原本的博客是用wordpress搭建的,当年七牛云还是免费时用它做的CDN,但是现在收费了,如果维持目前的博客流量的话每个月都要支付不少费用,于是只好把七牛云给停了= =,这也导致我博客里面很多图片都访问不了,页面布局也出问题了,不想折腾了,而且wordpress用起来总觉得不顺手,还是自己做一个吧~ 计划 最近刚好在学SpringBoot啥的,本来想用springboot搭建一个博客

2022-07-18 · Web前端 · 约 1 分钟

讨论

共 0 条

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

加载评论中…