开发者

Is typedef ever required in C?

Typedef is very useful for portable names, tag names (typedef struct foo Foo;) and keeping complicated (function) declarations readable (typedef int (*cmpfunc)(const void *, const void *);).

But are there situations in C where a typedef is really truly needed? Where you cannot accomplish the same by simple writing out the derived type.

To clarify a bit: I mean for language users, not implementers. The whole of stdint.h is a good example of the second category.

Conclusion

Thanks for all your input. I think I can summarise it as:

  • The C99 library needs typedef to implement the various (u)intN_t types.
  • On C89 you really want to use typedefs yourself to create similar portable types.
  • You might need typedef when using the va_arg macro, but I doubt you will encounter these derivative types in practise开发者_如何学运维.


Thanks all for your answers. I looked some more myself and found this in C99, J.2 Undefined behaviour:

The behavior is undefined in the following circumstances: [...]

  • The type parameter to the va_arg macro is not such that a pointer to an object of that type can be obtained simply by postfixing a * (7.15.1.1).

So when you want to pass/extract complicated derived types to va_arg such as int (*)[] you need to typedef this type to something for which this is possible (updated/corrected):

typedef int (*intarrptr)[4];
intarrptr x = va_arg(ap, intarrptr);

Since it is difficult to find and actual useful case for this one might conclude that it is not a strong argument for the necessity of typedef.


A typedef is, by definition, an alias. As such, you always could replace the alias with the actual type. It wouldn't be an alias otherwise.

That doesn't mean avoiding typedef would be a good idea.


From wikipedia:

typedef is a keyword in the C and C++ programming languages. The purpose of typedef is to assign alternative names to existing types, most often those whose standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another.1

Based on that definition, no it's never required as you can always just write out the expanded form. However, there may be macro definitions which choose a typedef to use based on platform or something else. So always using the expanded form may not be very portable.


it is definitely required, a good example is size_t which is typedef'd on various platforms.


The keyword typedef is definitely needed in test suites that check a C compiler for ISO-C compliance.

In code that is not explicitly supposed to use typedef (like the test suite above) it is often very helpful but never essential because it only establishes an alias to another type. It does not create a type.

Finally, I would not count things like avoiding a compiler limit on preprocessed source file size through the abbreviation typedefs can offer.


Yes. Integer types that have to be a fixed size. e.g. int32_t (and you want your code to have a fighting chance of being portable).


The offsetof() macro requires a structname and a membername, and thus cannot be used with inline struct definitions. Well, maybe on your implementation it can, but it isn't guaranteed by the C standard.

Update: As pointed out in the comments, this is possible:

struct blah { int a; int b; };
x = offsetof(struct blah, a); // legal

But inlining the struct definition is not:

x = offsetof(struct {int a; int b;}, a); // illegal

The former does not contain the keyword typedef, but inlining the struct definition is not possible either. Which version is indicated by "simple writing out the derived type" is unclear.


No.

A typedef does not create a truely new type as say a class in C++, it merely creates a type alias - a single identifier for something else that already exists. In a typedef, you define no new behaviour, semantics, conversions or opeators.


No. I think I'm safe in saying that!


I know of no cases where typedef is explicitly needed in C.


doesn't 'extremely useful' mean requied?

not even hard for me to think about a project with struct for each struct member,pointer,arguement,and so on.


To me, typedef provides abstraction. It keeps my code clean and very easy to understand. You can live without typedef just like you can live without all high level languages and sticking with assembly or machine language.


Individual programmers are not required to create their own typedefs. There's no universal rule saying I can't write things like

int *(*(*x())[5])();

if I so desire (although my company's coding standards may frown on it).

They're all over the standard library, however (FILE, size_t, etc.), so you really can't avoid using typedef names.


We use typedef to at work keep code platform independent. Like for example, we do something like

typedef unsigned char SHORT;

this makes code more readable and easy to port.


Without typedef, you have to use the keyword struct everytime if you use a struct in declarations. With typedef, you can omit this.

struct Person
{
  char * name;
};

struct Person p; /* need this here*/

typedef struct _Person
{
  char * name;
} Person;

Person p; /* with typedef I can omit struct keyword here */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜