当前位置: 首页> 财经> 访谈 > 快速搭建网站wordpress_合肥建筑材料市场信息价官网_关键词搜索工具有哪些_bt搜索引擎

快速搭建网站wordpress_合肥建筑材料市场信息价官网_关键词搜索工具有哪些_bt搜索引擎

时间:2025/7/13 13:13:58来源:https://blog.csdn.net/wenhao_ir/article/details/144460982 浏览次数:0次
快速搭建网站wordpress_合肥建筑材料市场信息价官网_关键词搜索工具有哪些_bt搜索引擎

C/C++中可以用关键词typedef为一个类型定义别名,而且可以一次定义多个别名哟,别名与别名之间用逗号分隔,并且一般别名和类型的指针类型也可以同时被定义哟。

目录

  • 示例1-为int类型定义三个别名
  • 示例2-为int类型同时定义一般别名和指针类型别名
  • 示例3-为结构体类型定义三个别名
  • 示例4-为结构体类型定义两个一般别名和一个指针别名

示例1-为int类型定义三个别名

typedef int a1, a2, a3;

有了这个定义,那么有下面的用法

a1 x = 5;  // 等价于 int x = 5;
a2 y = 10; // 等价于 int y = 10;
a3 z = 15; // 等价于 int z = 15;

示例2-为int类型同时定义一般别名和指针类型别名

typedef int a1, a2, a3, *b1;

这段代码使用了 typedef,它的作用是为类型创建别名。这条语句的解析如下:

typedef int a1, a2, a3, *b1;

解析

  1. typedef int a1;

    • 定义了 a1int 类型的别名。
  2. typedef int a2;

    • 定义了 a2int 类型的别名。
  3. typedef int a3;

    • 定义了 a3int 类型的别名。
  4. typedef int *b1;

    • 定义了 b1int*(指向 int 类型的指针)的别名。

使用示例
定义变量时:

a1 x = 10;    // 等价于 int x = 10;
a2 y = 20;    // 等价于 int y = 20;
a3 z = 30;    // 等价于 int z = 30;b1 pointer1 = &x;  // 等价于 int *pointer1 = &x;

总结
这句代码的作用是:

  • int 类型定义了三个一般别名:a1a2a3
  • int*(指针类型)定义了一个别名:b1

示例3-为结构体类型定义三个别名

typedef struct Region {int iLeftUpX;int iLeftUpY;int iWidth;int iHeigh;
} Zone, Area, Range;

这段代码为结构体类型struct Region定义了三个别名,分别为Zone、Area、Range。
有了这些定义,那么有:

Zone z = {0, 0, 100, 200};   // 定义一个 Zone 类型的变量
Area a = {10, 20, 300, 400}; // 定义一个 Area 类型的变量
Range r = {5, 5, 50, 50};    // 定义一个 Range 类型的变量

等效于:

struct Region z = {0, 0, 100, 200};
struct Region a = {10, 20, 300, 400};
struct Region r = {5, 5, 50, 50};

示例4-为结构体类型定义两个一般别名和一个指针别名

typedef struct Region {int iLeftUpX;int iLeftUpY;int iWidth;int iHeigh;
} Zone, Area, Range, *district;

Zone 是 struct Region 类型的别名。
Area 是 struct Region 类型的别名。
Range 是 struct Region 类型的别名。

*district:这个部分定义了一个指向 struct Region 类型的指针类型别名 district。它表示:district 是指向 struct Region 类型的指针类型。

用法:

Zone z1 = {0, 0, 100, 200};   // 使用 Zone 定义 struct Region 类型的变量
Area a1 = {10, 10, 150, 250}; // 使用 Area 定义 struct Region 类型的变量
Range r1 = {20, 20, 200, 300}; // 使用 Range 定义 struct Region 类型的变量district p = &z1;  // 使用 district 定义一个指向 struct Region 的指针
关键字:快速搭建网站wordpress_合肥建筑材料市场信息价官网_关键词搜索工具有哪些_bt搜索引擎

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: