The typedef detailed in C language

Source: Internet
Author: User
Tags arrays constant int size readable requires

A typedef declaration, called a TypeDef, creates a new name for an existing type. For example, people often use T ypedef to write more beautiful and readable code. The so-called aesthetics, meaning that the typedef can hide clumsy syntax constructs and platform-related data types, thereby enhancing portability and future maintainability.

The most common use of a typedef is to create a type name that is easy to remember, and use it to archive the programmer's intent. The type is now declared in the variable name, located on the right side of the ' typedef ' keyword. For example: typedef int size;

This declaration defines a synonym for int, with a name of size. Note that a typedef does not create a new type. It simply adds a synonym to an existing type. You can use size in any context that requires int.

A typedef can also mask conforming types, such as pointers and arrays. For example, you don't have to repeatedly define an array of 81 character elements as follows:

CHARLINE[81]; Char text[81];

Defines a typedef that you can do whenever you want to use an array of the same type and size: typedef char LINE[81]; line text, Secondline;

There is another important use of TypeDef, which is to define machine-independent types, for example, you can define a floating-point type called real, and on the target machine it can achieve the highest precision:

typedef long double Real;

On a machine that does not support long double, the typedef appears to be the following: typedef double real;

And, on a machine that does not have a double, the typedef will look like this: typedef float REAL;

Instead of making any changes to the source code, you can compile the application with real type on each platform. The only thing to change is the typedef itself. In most cases, even this tiny change can be done automatically through wonderful conditional compilation. Isn't it? Standard libraries use typedef extensively to create such platform-independent types: size_t, Ptrdiff and fpos_t are examples of this. In addition, the typedef such as St D::string and Std::ofstream also hide the long, incomprehensible template-specific syntax, such as: Basic_string<char, Char_traits<char> Allocator<cha r>> and Basic_ofstream<char, char_traits< char>>. Note 1: Note the difference between typedef and define

typdef int * A,b

The a,b type is the same

define INT * A,b

The a,b type differs in NOTE 2:

typedef int (*MAC_LISTEN_FUNC) (int*,char*); Mac_listen_func Mlf1,mlf2;

Equivalence definition:

Int (*MLF1) (int*,char*); Int (*MLF1) (int*,char*);

The above way to use more convenient, readability is also better.

A typedef declaration, called a TypeDef, creates a new name for an existing type. For example, people often use a typedef to write more beautiful and readable code. The so-called aesthetics, meaning that the typedef can hide clumsy syntax constructs and platform-related data types, thereby enhancing portability and future maintainability. This article will make every effort to uncover the powerful features of the typedef and how to avoid some common pitfalls.

How do I create a platform-independent data type that hides awkward and incomprehensible syntax?

Use typedefs to create synonyms for an existing type.

The most common use of a type name typedef that defines memory is the creation of an easily remembered type name, which is used to archive the programmer's intent. The type is now declared in the variable name, located on the right side of the ' typedef ' keyword. For example:

typedef int size;

This declaration defines a synonym for int, with a name of size. Note that a typedef does not create a new type. It simply adds a synonym to an existing type. You can use it in any context that requires int

Size

void measure (Size * psz);

Size array[4];

Size len = File.getlength ();

Std::vector <size> vs;

A typedef can also mask conforming types, such as pointers and arrays. For example, you don't have to repeatedly define an array of 81 character elements as follows:

Char line[81];

Char text[81];

Defines a typedef that you can do whenever you want to use an array of the same type and size:

typedef char LINE[81];

line text, Secondline;

Getline (text);

Similarly, you can hide the pointer syntax as follows:

typedef char * PSTR;

int mystrcmp (PSTR, PSTR);

This will take us to the first typedef trap. Standard function strcmp () has two parameters of type ' constch ar * '. Therefore, it may mislead people to declare mystrcmp () as follows:

int mystrcmp (const PSTR, const PSTR);

This is wrong, in order, ' constpstr ' is interpreted as ' char * const ' (a constant pointer to cha R), not ' const char * ' (a pointer to a constant char). This problem is easy to solve:

typedef const CHAR * CPSTR;

int mystrcmp (CPSTR, CPSTR); It's right now.

Remember: Whenever you declare a typedef for a pointer, add a const to the final typedef name so that the pointer itself is a constant, not an object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.