typedef和#define [英] typedef and #define
问题描述
如果我用#define替换typedef怎么办?
What will happen if i replace a typedef with a #define?
推荐答案
vivek写道:
vivek wrote:
如果我用#define替换typedef怎么办?
What will happen if i replace a typedef with a #define?
请参阅:
< http://c-faq.com/decl/typedefvsdefine.html>
See:
<http://c-faq.com/decl/typedefvsdefine.html>
vivek写道:
vivek wrote:
如果我用#define替换typedef怎么办?
What will happen if i replace a typedef with a #define?
你的代码会中断[1]。
[1]可能。法庭灾难为何?为什么用错误的
替换正确的构造?
-
灰烬燃烧的方式。 - 文艺复兴,/灰烬燃烧/
Hewlett-Packard Limited注册号:
注册办事处:Cain Road,Bracknell,Berks RG12 1HN 690597 England
Your code will break [1].
[1] Probably. Why court disaster? Why replace the right construct with
the wrong one?
--
"Ashes are burning the way." - Renaissance, /Ashes Are Burning/
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
3月4日凌晨4:56,vivek< gvivek2 ... @ gmail.comwrote:
On Mar 4, 4:56 am, vivek <gvivek2...@gmail.comwrote:
如果用#define替换typedef怎么办?
What will happen if i replace a typedef with a #define?
这取决于。让我们假设你已经为指针创建了一个typedef
int:
typedef int * iptr;
iptr px,py;
px和py都将输入int *。
现在我们用宏替换它:
#define IPTR int *
IPTR px,py;
预处理后,这将扩展为
int * px,py;
在这种情况下,只有px将被输入int *; py将是一个简单的int。
因此,对于这种特殊情况,你会得到不同的结果。
某些typedef很难替换用宏。对于
的例子,请参考
typedef int *(*(* foo)[20])(char c);
foo是指向20元素指针数组的指针的同义词
函数接受char参数并返回指向int的指针。一个
简单的宏来替换这个typedef就好了,不是那么简单。
It depends. Let''s suppose you''ve created a typedef for a pointer to
int:
typedef int *iptr;
iptr px, py;
Both px and py will be typed int*.
Now we replace it with a macro:
#define IPTR int *
IPTR px, py;
After preprocessing, this will expand to
int * px, py;
In this case, only px will be typed int*; py will be a plain int.
So, for this particular case, you get different results.
Some typedefs would be difficult to replace with a macro. For
example, take
typedef int *(*(*foo)[20])(char c);
foo is a synonym for "pointer to 20-element array of pointers to
functions taking a char parameter and returning a pointer to int." A
simple macro to replace this typedef would be, well, not so simple.
这篇关于typedef和#define的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!