简体   繁体   中英

Using of typedef struct

I want to know if there is any difference in performance, declaration, etc.

I have a file that called features.h, and it has a struct definition:

typedef struct feat_record
{
 ...
 ...
} Feature;

i want to use it in another file in a method, in the other file .h file I include features.h. And my question is, if there is a difference between declaring the method in the other file .h like this:

void myMethod(Feature *f);

or like this:

void myMethod (struct feat_record *f);

Thanks

This is only coding style and there is no other difference than subjective, personal preference.

The typedef version is often encouraged for programs that are in the borderland between C and C++, since it makes the style consistent with C++ types (in C++ the struct keyword is superfluous when declaring objects of a struct).

The struct tag version is mainly encouraged by the Linux camp and enforced by the Linux kernel code style guide (which does not provide much of a rationale beyond subjective opinions).

There is no obvious right or wrong, just pick the style you like and stick to it consistently.

The difference is that the typedef declaration of Feature (which may or may not include the definition of feat_record ) must precede the function declaration using it, whereas

void myMethod (struct feat_record *f);

stands on its own as an interface declaration; it does not depend on a preceding typedef or other declaration.

Nevertheless, it is better to declare struct feat_record; first, if only to silence an inevitable compiler warning. And such a declaration is required before the function declaration in the file that implements myMethod .

Ultimately, the difference is style, namely verboseness: you should decide whether you want to refer to all structs as struct in your program. It means more typing, but serves as a reminder to initialize things. (Or, if you prefer, a metadata tag for things that must be handled as a struct .)

As for the compiled program, there is no conceivable difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM