A typedef of C language notes

Source: Internet
Author: User

typedef this keyword is used to give a new name to a type, unlike define, a typedef gives a symbolic name that is limited to a type, not a value. Here is an example:

typedef short WCHAR;

typedef defines the short type as WCHAR type, then encounters WChar a later;  In fact, it is short A; If there is no typedef this keyword, WCHAR is a variable name, plus Typedef,wchar becomes the type name. The type name also follows the naming convention for identifiers, and usually adds a _t suffix that represents the type.

What is the use of this? Just because the basic type of short is out of the question, the strong point of the TypeDef is to redefine the compound type, give a new name to the compound type, and later declare the variable directly with the new name, without having to write a bunch of long compound types. Of course, in some cases, you also need to rename the base type, such as platform porting.

The usage of a typedef is described in several situations below.

1. Pointers

Consider such a statement:

typedef char * string;

Then a string is a pointer type, which points to the char type. So

String A, B;

It defines two pointer variables, all of which point to a variable of type char. Equivalent:

Char *a, *b;


2. Arrays

typedef int ARRAY[5];

Array is a new type, which represents an array of 5 numeric values of type int, array new; is equivalent to an int new[5];

This form is a bit laborious to understand and can be used in the above method: if there is no typedef, then array is a normal array name; with a typedef, the array becomes a type name.


3. Structural body

Structure is the main battlefield of TypeDef, because the general structure is too long, only renaming can simplify the code. For example, the following:

struct {

int A;

Double *b;

char name;

} sth;

Then, while declaring the variable, it can be: struct STH one, and the other; But it's still a bit annoying to write a struct every time. The good idea is this:

typedef struct {

int A;

Double *b;

char name;

} sth;

Then you can directly sth one, the other; The


4. Functions

You're right, typedef can even define function types!! As follows:

typedef int func (double var);

This defines a function type Func, which can then be used to define the variable with Func. The understanding of this sentence with the above, first remove the TypeDef, plus typedef, see the meaning of the Func change.

Once a variable is defined, it can be used directly as a function name , such as:

Func A;

Double b = 2;

int C = A (2);


These are some of the main uses of typedef.

A typedef of C language notes

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.