C language declaration and common usage of typedef

Source: Internet
Author: User

Priority rules declared in C Language

A declares that it reads data starting from its name and then reads data in order of priority.

The priority of B is:

B .1 The Section enclosed in parentheses in the statement

B .2 suffix OPERATOR:

Parentheses () indicate that this is a function, and

Square brackets [] indicate that this is an array

B .3 prefix OPERATOR: asterisk * indicates "pointer"

C. If the const and volatile keywords follow the type specifiers (int long, etc.), the scope type specifiers. In other cases, the const and volatile keywords apply to the pointer stars next to the left side of the const and volatile keywords.

Through the above rule analysis, the C language statement is as follows:

Char * const * (* next )();

Next is a pointer pointing to a function. This function returns a pointer to a constant pointer of the char type.


Typdef: it introduces a new name for a type and does not create a new type.

In general, typedef is used to represent pointers to other things in a concise manner. A typical example is the declaration of the signal () prototype.

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

Signal is a function that returns a function pointer. The function to which this function pointer points accepts an int parameter and returns void.

The following changes can be made through typedef:

Typedef void (* sighandler_t) (int );
Sighandler_t signal (int signum, sighandler_t handler );


C language has multiple namespaces:

Label name)

Tag: This namespace is used for all structures, enumeration, and union.

Member name: each structure or union has its own namespace.

Others

In the same namespace, any name must be unique, but the same name may exist in different namespace. Because each structure or combination has its own namespace, the same name can appear in many different structures.

Typdedef struct my_tag {int I;} mytype;

Struct my_tag var1;

Mytype var2;

This typedef declaration introduces the name mytype as a shorthand for "struct my_tag {int I. However, the structure tag my_tag is introduced at the same time. Adding a keyword struct before it can indicate the same meaning.

Typedef struct fruit {int weight;} fruit; Statement 1

Struct veg {int weight;} veg; Statement 2

They represent completely different meanings. Statement 1 declares the structure type fruit declared by the structure tag fruit and typedef. The actual effect is as follows:

Struct fruit mandarin;

Fruit mandarin;

Statement 2 declares the structure tag veg and the variable veg. Only the structure tag can be used in future declarations, as shown in

Struct veg potato;

If you try to use a declaration like veg cabbage, it will be an error. This is similar to the following statement:

Int I;

I j;


The difference between typedef and macro text replacement. Typedef can be regarded as an "encapsulation" type-after declaration, nothing can be added to it. The difference between it and macro is reflected in two aspects:

1. The macro type name can be extended with other type specifiers, but the type name defined by typedef cannot.

# Define peach int

Unsigned peach I; no problem

Typedef int peach;

Unsigned peach I; Syntax Error

2. In the declaration of several consecutive variables, the type defined by typedef can ensure that all the variables in the Declaration are of the same type, while the type defined by # define cannot be guaranteed.

# Define int_ptr int *

Int_ptr chalk, cheese;

Macro Scaling

Int * chalk, cheese;

Chalk is a pointer and cheese is an integer.

Typedef int * int_ptr;

Int_ptr chalk, cheese;

Chalk and cheese are all integer pointers


Enumeration type

Enum sizes {small = 7, medium, large = 10, humungous };

By default, the integer value starts from scratch. If a value is assigned to an identifier in the list, the value of the identifier next to it is 1 greater than the assigned value, and so on.

Enumeration members can be used directly as macros, which has an advantage over macro enumeration: # The name defined by define is generally discarded during compilation, while the enumeration name is usually visible in the debugger, they can be used when debugging code.






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.