c++ - typedef 只是代码中的字符串替换还是其他内容?

标签 c++ c syntax

我很想知道 typedef 究竟是如何工作的。

typedef struct example identifier;

identifier x;

在上面的语句中,'identifier' 只是在代码中用 'struct example' 替换(类似于字符串替换)吗?如果不是,typedef 在这里做什么?

请指教!

最佳答案

不,这不是字符串替换——那是宏。它为该类型创建一个别名。

typedefs are preferred over macros对于自定义类型,部分原因是它们可以正确编码指针类型。

typedef char *String_t;
#define String_d char *
String_t s1, s2;
String_d s3, s4;

s1, s2, and s3 are all declared as char *, but s4 is declared as a char, which is probably not the intention.

关于c++ - typedef 只是代码中的字符串替换还是其他内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3263252/

相关文章:

c++ - libcurl 示例 httpcustomheader.c 是否存在错误(显示不良做法)或者我是否遗漏了什么?

c++ - char *p_c = new char ['1' , '2' , '3' , '4' ]; 的含义

c - 将结构传递给函数

r - data.table 语法遇到一些问题

swift - let 在 catch 表达式中的作用是什么?

c++ - 通过构造函数动态确定类实现

c++ - 你如何获得cpp中特定 vector 行的大小?

c - 时间段错误(0);

C: 数组指针指向自己的地址同时携带一个值?

c# - 如何检查字符串是否具有正确的html语法