Typedef问题 [英] Typedef question

查看:62
本文介绍了Typedef问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快点? :-)关于Typedef的问题。

在K& R中有一个非常简短的讨论(p146)。谷歌搜索

这个群体,对于这些问题有一个令人惊讶的缺乏。

从其中一个主题中,有一些合理的建议(无论如何对我来说)

不要隐藏typedef背后的指针。

所以,我可以问一下当使用typedef真正有意义的时候吗?


对不起,如果这有点笼统,但没有提供的练习(不是我要求的
!!)。

解决方案

< blockquote> mdh写道:


快点? :-)关于Typedef的问题。

在K& R中有一个非常简短的讨论(p146)。谷歌搜索

这个群体,对于这些问题有一个令人惊讶的缺乏。

从其中一个主题中,有一些合理的建议(无论如何对我来说)

不要隐藏typedef背后的指针。

那么,我可以问一下使用typedef时组是否真的有意义吗?



typedef的一个重要用途是处理从机器到机器的C'本机类型的可变性。例如,Park和

米勒的最小标准随机数发生器使用数字作为

大到2147483646。许多机器的'int类型将足以支付这个

的幅度,但在某些情况下你可能需要求助。你可以

只需在代码中的任何地方使用很长时间,但在某些机器上可能会因为b $ b b而过度杀伤。这是一个可能的解决方案:


#include< limits.h>

#if INT_MAX> = 2147483646

typedef int MSint; / * int足够* /

#else

typedef long MSint; / * int太小;使用长* /

#endif


....然后使用MSint编写代码。 typedef

封装了您选择的适当本机类型,以便在手边的环境中使用。它并没有解决所有问题 - 对于

例子,如果你使用printf()或scanf()这些数字,你需要选择正确的格式字符串 - 但它可以平滑

a很多机器到机器的差异。


另一个重要的用途是通过分解提高可读性

将声明复杂化为可管理的部分。经典的例子

这是signal()函数的声明:


void(* signal(int sig,void(* func)(int )))(int);


如果使用typedef,大多数人会发现代码更具可读性

(有多种方式可以这样做):


typedef void(SigHandler)(int sig);

SigHandler * signal(int sig,SigHandler * func);


另一种用法是将暗示的单字名称附加到struct

和union类型,如


typedef struct hashItem {

unsigned int hashValue;

const void * itemPointer;

struct hashItem * nextItem;

} HashItem;


人们对这是否是个好主意不同。有些人觉得这个东西的结构不能被隐藏(你要使用.b-b使用。而且 - 毕竟),所以尝试隐藏它是

虚弱甚至可能是混淆的。其他人觉得它比#struct hashItem更容易编写和读取HashItem,并且使用较短的短语来提高可读性,因此更容易编写和读取HashItem。我在后一个阵营

(特别是当结构或联合的性质是一个

库的私有而且只有`HashItem *''被导出到客户),但是我的

偏好并不是那么强烈,以至于我会拿起武器来对抗那些不相信的人。你将不得不在这个问题上咨询自己的口味。


最后,指针的东西。即使图书馆在指向私人结构的指针中流动,他们指点的事实通常也是打电话者必须知道的事情,如果没有别的原因,那么测试

a返回值对NULL。出于这个原因,我更愿意说

函数返回一个'HashItem *'而不是`HashItemPointer'';

后者只是让你更难分辨出你在处理什么。

(这个立场是否与我对'HashItem'的偏好不一致

`struct hashItem''?可能,但正如我之前所说,我不打算对它进行战争。)


- -
Er*********@sun.com


mdh写道:


快点? :-)关于Typedef的问题。

在K& R中有一个非常简短的讨论(p146)。谷歌搜索

这个群体,对于这些问题有一个令人惊讶的缺乏。

从其中一个主题中,有一些合理的建议(无论如何对我来说)

不要隐藏typedef背后的指针。

所以,我可以问一下当使用typedef真正有意义的时候吗?


对不起,如果这有点笼统,但是没有提供练习(不是我要求的b $ b)!



Typedef至少有两个主要用途。最常见的一个是

允许你编写使用特定类型的代码,当你想要它应该使用的类型可能在不同的

上下文。一个简单的例子是size_t,它是由

C标准库本身设置的typedef。它可能是一个

实现中的unsigned char,以及unsigned long long。另外一个。但是,你将
从一个

实现移动到另一个实现时不需要对代码进行任何更改,因为更改发生在C

typedef size_t的标准头文件。

我见过的第二个用途是简化类型声明。如果你
大量使用unsigned char,那么使用起来很方便


typedef unsigned char uchar;


同样,使用方便


typedef struct {int i;双d; } intdouble;


而不是


struct intdouble {int i;双d;};


主要是因为它允许你使用intdouble

而不是struct intdouble来引用类型。这对于习惯使用C ++的人来说特别有吸引力,因为只需要在类型的定义中使用关键字struct

,而不是在使用它时。 br />

而且,正如您已经看到的那样,typedef可以用来简化

依赖于函数指针的代码。


9月2日,12:47 * pm,Eri​​c Sosman< Eric.Sos ... @ sun.comwrote:


mdh写道:


快点? :-)关于Typedef的问题。



* * * typedef的一个重要用途是处理从机器到机器的C'本机类型的可变性



* * *另一个重要的用途是通过将复杂的声明分解成可管理的部分来提高可读性。


Eric.Sos。 .. @ sun.com



谢谢Eric的解释。


A quick ? :-) question about Typedefs.
There is a very brief discussion about this in K&R ( p146). Googling
this group, there is a surprising dearth of questions about these.
From one of the threads, there is sound advice ( to me at any rate)
not to hide pointers behind typedefs.
So, may I ask the group when the use of typedefs really makes sense?

Sorry if this is somewhat general, but there are no exercises ( not
that I am asking for any!!) provided.

解决方案

mdh wrote:

A quick ? :-) question about Typedefs.
There is a very brief discussion about this in K&R ( p146). Googling
this group, there is a surprising dearth of questions about these.
From one of the threads, there is sound advice ( to me at any rate)
not to hide pointers behind typedefs.
So, may I ask the group when the use of typedefs really makes sense?

One important use for typedef is to deal with the variability
of C''s native types from machine to machine. For example, Park and
Miller''s "Minimal Standard Random Number Generator" uses numbers as
large as 2147483646. Many machines'' int type will suffice for this
magnitude, but on some you may need to resort to long. You could
just use long everywhere in the code, but on some machines that may
be overkill. Here''s a possible solution:

#include <limits.h>
#if INT_MAX >= 2147483646
typedef int MSint; /* int is enough */
#else
typedef long MSint; /* int too small; use long */
#endif

.... and then write the code using MSint throughout. The typedef
encapsulates your choice of the appropriate native type to use in
the environment at hand. It doesn''t solve every problem -- for
example, if you use printf() or scanf() with these numbers, you
need to choose the right format strings -- but it can smooth out
a good many machine-to-machine differences.

Another important use is to improve readability by decomposing
complicated declarations into manageable pieces. The classic example
of this is the declaration of the signal() function:

void (*signal(int sig, void (*func)(int)))(int);

Most people will find the code more readable if a typedef is used
(there''s more than one way to do this):

typedef void (SigHandler)(int sig);
SigHandler *signal(int sig, SigHandler *func);

Another use is to attach suggestive one-word names to struct
and union types, as in

typedef struct hashItem {
unsigned int hashValue;
const void *itemPointer;
struct hashItem *nextItem;
} HashItem;

People differ about whether this is a good idea or not. Some feel
that the struct-ness of the thing cannot be hidden (you''re going to
use . and -with it, after all), so the attempt to hide it is
feeble and maybe even obfuscatory. Others feel that it''s quicker and
easier to write and read `HashItem'' than `struct hashItem'', and that
using the shorter phrase improves readability. I''m in the latter camp
(especially when the nature of the struct or union is private to a
library and only a `HashItem*'' is exported to the clients), but my
preference isn''t so strong that I''d take up arms against the
unbelievers. You''ll have to consult your own tastes on this one.

Finally, the pointer thing. Even when a library traffics in
pointers to private structs, the fact that they''re pointers is usually
something the caller must know, if for no other reason than to test
a returned value against NULL. For this reason, I''d prefer to say
that a function returns a `HashItem*'' than a `HashItemPointer''; the
latter just makes it harder to discern what you''re dealing with.
(Is this stance inconsistent with my preference for `HashItem'' over
`struct hashItem''? Probably, but as I said earlier I''m not going to
war over it.)

--
Er*********@sun.com


mdh wrote:

A quick ? :-) question about Typedefs.
There is a very brief discussion about this in K&R ( p146). Googling
this group, there is a surprising dearth of questions about these.
From one of the threads, there is sound advice ( to me at any rate)
not to hide pointers behind typedefs.
So, may I ask the group when the use of typedefs really makes sense?

Sorry if this is somewhat general, but there are no exercises ( not
that I am asking for any!!) provided.

Typedefs serve at least two main purposes. The most common one is to
allow you to write code that uses a particular type, when you
anticipate that the type it should use might be different in different
contexts. A simple example is size_t, which is a typedef set up by the
C standard library itself. It might be ''unsigned char'' on one
implementation, and "unsigned long long" on another. However, you
don''t have to make any changes to your code when moving it from one
implementation to the other, because the change occurs inside the C
standard headers that typedef size_t..

The second use I''ve seen is for simplifying type declarations. If you
make a lot of use of unsigned char, it''s convenient to use

typedef unsigned char uchar;

Similarly, it''s convenient to use

typedef struct { int i; double d; } intdouble;

rather than

struct intdouble { int i; double d;};

mainly because it allows you to refer to the type using "intdouble"
rather than "struct intdouble". This is especially attractive to
people who are used to C++, where the keyword struct would be needed
only in the definition of the type, and not when using it.

And, as you''ve already seen, typedef can be used to simplify code that
relies upon function pointers.


On Sep 2, 12:47*pm, Eric Sosman <Eric.Sos...@sun.comwrote:

mdh wrote:

A quick ? :-) question about Typedefs.

* * *One important use for typedef is to deal with the variability
of C''s native types from machine to machine.
* * *Another important use is to improve readability by decomposing
complicated declarations into manageable pieces.

Eric.Sos...@sun.com

thank you Eric for that explanation.


这篇关于Typedef问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆