Chapter 3 Description and Types

xiaoxiao2021-03-06  15

Chapter 3 Description and Types

This chapter describes the description and initialization of variables, functions, and types. The C language includes a standard set of standard data types.

You can also increase your own data type, called "Detective Type", which means a new type by a defined type.

The topic discussed is as follows:

* Description Overview

* Storage class

*indicator

* Type modifier

* Recommendation and variable description

* Explanation complex statement

*initialization

* Basic type of storage

* Incomplete type

* Type definition description

* Extended storage class properties

Description Overview

A "description" indicates the interpretation and properties of a set of identifiers.

The description of the object or function that causes the identifier is called "definition". C description of variables, functions, and types have such syntax:

grammar

Description:

Description Indicator Initialization Description Table OPT

Description Indicator:

Storage class indicator attribute sequence OPT

Description indicator OPT

/ * Property sequence OPT is Microsoft Special Office * /

Type Indicator Description Indicator OPT

Type modifier description indicator OPT

Initialization Description:

Initialization specifier

Initialization Description

Initialization specifier

Initialization specifier:

Specify

Reference = Initializer

Note: The grammatical explanation is no longer repeated. The statements used later are usually starting with the end of the specifier.

Initialization Description Table Description contains the named identifier; initialization is the referred to as the initializer. Initialization Description Table is a sub-syndrome sequence, each with additional type information or an initializer or both. The specifier contains any description of the identifier. Description Indicator Non-endue consists of a portion that is the minimum type of entity type indicated by the type sequence and pointed out of the connection, storage period, and the indicator. Thus, some combination of storage class indicators, type indicators, type modifiers, setups, and initializer are explained.

Note You can include one or more optional properties listed in the properties sequence. These Microsoft Specific Attributes implement the variation of the function, which discusses in detail in this book. For the attribute list, see Appendix A "C language syntax summary" behind this volume. In a general format illustrated in a variable, the type indicator gives the data type of the variable. The type indicator can be composite, can be modified with const or volatile. The specifier gives a variable name, indicating a modification of a array or pointer type. E.g:

INT const * fp;

Note Pointers in a const) int value that names FP is unmodified. You can use multiple comma-separated instructions to define more than one variable in one instruction.

A description must have at least one specifier, or its type indicator must illustrate a member of a structural flag, a federation, or an enumerator. One specifier is an identifier that can be modified by square brackets ([]), asterisk (*) or parentheses (()), respectively, to describe an array, pointer or function. This indicator is also an identifier when you explain the simple variables (such as characters, integers, and floating point items) or some simple variables. For more information, see "Recommend and Variable Description" later in this chapter.

All definitions are implied, but not all instructions are defined. For example, an explanation begins with an extern storage class indicator is "reference" instead of a "definition" description. If the external variable is referenced before it is defined, or if it defines in another source file, you must use an extern description. "Reference" instructions are not allocated, and the variables in the description cannot be initialized.

A storage class or a type (or both) is required in the variable description. In addition to __Declspec, only one storage class indicator is allowed in one instruction, not all of the storage clauses are allowed in each context.

The __Declspec store allows for use with other storage class indicators, allowing multiple times. A description of the stored class indicator affects how the item is stored and initialized, as well as which of the program can reference the item.

The storage class indicator terminal defined in C includes auto, extern, register, static, and typedef. In addition, Microsoft C includes a storage class indicator terminal method__declspec. All storage clauses that except TypedEf and __Declspec are discussed in the "Storage Class". Information about typedef is referred to as "Typedef Description" later in this chapter. See "Expanding Storage Class Properties" for the information about _ _declspec.

In a source program, it is important to have other instructions for determining variables. There are multiple reintegration, but there is only one definition. But a definition can appear in multiple conversion units. For internal connections, this rule is applied to each conversion unit, because the object within the internal connection is unique to a conversion unit. For external connections, this rule is applied to the entire program. For more information on visibility, see Chapter 2, Survival, Scope, Visibility, and Connections. The type indicator provides information about the identifier data type, the default type indicator is int. For more information, see "Type Indicators" later in this chapter. Type indicators can also define type flags, structures, and combined composition components and enumerations. For more information, see "Enumeration Description", "Structure Description", "Structure Description", and "Joint Description" later in this chapter.

There are two type modifiers Terminal: const and volatile. These modifiers indicate additional type characteristics, which are only related to the objects that access to the type of the L value. For more information on const and volatile, see "Type Modifiers" later in this chapter. For the definition of the l value, see "L Values ​​and R Value Expressions" in Chapter 4 "Expression and Assignment".

Store

The "storage class" of a variable determines that the item is "global" survival or "local" survival. C calls these two survival: "static" and "automatic". An item with global survival exists throughout the program and has a value. All functions have global survival.

Automatic variables or variables having local lifetimes, allocate new storage at each block to define their blocks. This variable no longer has meaningless values ​​when returned.

C provides the following storage class indicator:

grammar

Storage class indicator:

Auto

Register

Static

Extern

Typedef

_ _DECLSPEC (Extension Description Modifier Sequence) / * Microsoft Specific * /

In addition to __Declspec, you can only use one storage class indicator in one instruction indicator. If there is no storage class indicator, an automatic object is established in a block.

Items description with Auto or Register indicator have a local life. Items description with Static or Extern indicators have global survival.

Because Typedef and __Declspec semantics are different from other 4 storage clause ends, they are discussed separately. See "Typedef Description" for specific information about typedef.

See "Expanding Storage Class Properties" for specific information about __DECLSPEC.

Variables and functions indicate that the location in the source file also affects storage classes and visibility. All function definitions are described outside the "external layer". The function defines inside instructions in "internal layer".

The precise meaning of each storage class indicator depends on two factors:

This explanation occurs on the external layer or the internal layer. This item is a variable or a function. The following two "The storage class indicator" and "internal layer descriptions description" describes the storage class indicator terminal in each description and explains the lack of ignore the memory class indicator from the variable. Provincial behavior. "Function Description of Store" discusses the use of storage class indicators in a function.

External layer description of the storage class indicator

The external variable is a variable of the file range, which is defined outside of any function, which is available for many functions. The function can only be defined outside the external layer, so it cannot be nested. By default, all the external variables and functions of the same name points to the same object, which means they have "external connections" (you can use the Static keyword to overwrite it, see the details of this chapter).

Variables in the outer layer illustrate a reference definition ("definition description") or otherwise defined variables ("Reference Description").

An external variable description (implicit or explicit) of an initialization variable is a definition of this variable. There can be several formats in the definition of the external layer:

* You use the Static storage class indicator description variable. You can explicitly initialize the Static variable in a constant expression, as described in "Initialization". If you omit the initializer, the variable is initialized to 0, for example, there are two statements that define the variable K here.

Static Int K = 16

Static Int K;

* A variable that is explicitly initialized in the external layer. For example, int J = 3; is a definition of variable j.

When the variable is described in the external layer (that is, outside all functions), you can use the Static or Externary storage class indicator or omit the storage class indicator. You can't use Auto and Register storage class indicator terminals on the external layer.

Once a variable is defined in the external layer, it is visible in the remaining conversion unit. This variable is invisible in front of the same source file. It is also invisible in other source files of the program unless there is a reference to make it visible, as explained below. Static's related rules include:

* All blocks do not have a Static keyword mode to keep their values ​​throughout the program. To limit their access to a specific conversion unit, you must use the Static keyword, which gives them "internal connection". In order to make them a global, explicitly store or use keyword extern (see rules in the table below). Internal and external connections are discussed in "Connections" in Chapter 2 "Program Structure".

* You can define only one variable in an external layer in a program that can define additional variables with the same name and STATIC storage class indicator in different conversion units. Since each Static definition is only visible in its own conversion unit, there is no conflict. This provides a useful way of this identifier name that implements sharing in a function of a conversion unit, but is invisible to other conversion units.

* Static storage class indicator can also be applied to functions. If a function is static, its name is not visible outside of the file being explained.

The rules that use extern are:

* Extern Storage Class Instructions Describe a reference to variables in other places, you can use a extern description that makes it visible in other source files, or makes a variable in the same source file. Once you explain a variable in the external layer, this variable is visible in the remainder of the conversion unit that appears in the reference.

* For a valid extern reference, the variables referred to must be defined once and only once at the external layer. This definition (no extern storage class) can be in the conversion unit of any configuration program.

example

The following example explains the external description:

/ *******************************************************************

Source File ONE

********************************************************* /

EXTERN INT i; / * Reference I, defined below * / void next (void); / * Function prototype * / void main () {i ; printf ("% d / n", i); / * i is equal to 4 * /. NEXT ();} int i = 3; / * i definition * / void next (void) {i ; Printf ("% d / n", i); / * i is equal to 5 * / other ();}

/ *******************************************************************

Source File Two

********************************************************* /

Extern Int i; / * Reference I, defined below * / / * i in the first source file * / void {i ; printf ("% d / n", i); / * i is equal to 6 * /}

The two source files in this example contain three external descriptions of i. There is only one "definition description", which is:

INT i = 3;

Define the global variable I and initialize it with the initial value 3. The "Reference" of the top I of the first source file will use extern to make the global variable before it definitions in this file. The reference description of I in the second source file makes it visible in this source file. If the definition of a variable is not provided in the conversion unit, the compiler assumes that there is one:

Extern INT X;

Reference description and a definition reference:

INT x = 0;

An additional conversion unit appears in the program.

All three functions Main, Next and Other perform the same function; they add I to 1 and print it. Print value 4, 5 and 6.

If the variable I has not been initialized, it is automatically set to 0. In this case, the printed values ​​1, 2 and 3. Information about variable initialization see "Initialization" later in this chapter.

Internal layer description of the storage class indicator

You can use any four storage class indicator terms in the internal layer. When you save the stored instrument from such a description, the default store is auto. So rarely see the auto keyword in a C program. Auto storage class indicator

Auto storage class indicator description an automatic variable, that is, a variable having local survival. A Auto variable is only visible in the block. Note The auto variable includes an initialization statement, as discussed in the "Initialization" section. Since variables with auto store are not automatically initialized, you can explicitly initialize it when you indicate them or assigned to the initial value in the statement of the block. The value of the unmelted auto variable is uncertain (for the local variable of the auto or register storage class, if an initialization statement is given, the initialization is performed each time you enter its range).

An internal STATIC variable (a STATIC with a part or block range) can be initialized with an address of any external or STATIC term, instead of initializing an address with other AUTO items because the AUTO item address is not a constant.

Register storage class indicator

Microsoft Special Office

The Microsoft C / C compiler does not agree with user requirements for register variables. But for compatibility, all other semantics associated with the Register keyword are allowed to pass the compiler. For example, you cannot apply a single address operator (&) to a register object, or you cannot use the register keyword to the array.

Microsoft End

Static storage class indicator

The variables described in the internal layer static storage class indicator have a global survival, but only in the blocks thereof are visible, for a constant string, use static is useful because it reduces the function that is often called. Frequent initialization of additional overhead.

If you do not explicitly initialize a Static variable, it defaults to 0, in one function, STATIC causes allocation storage and as a definition, the private, persistent storage provided by the internal static variable, only a single function is visible.

Extern storage class indicator

Variables descriptions descriptions description with extern storage are a reference to define the same name variable in any source file of the program. Internal extern descriptions are visible to the externally layer variables in this block. One variable description by extern keyword will not be seen in the block described in the external layer.

example

This example illustrates internal and external layers:

#include int i = 1; void Other (void); void main () {/ * reference i, defined above * / extern int i; / * Initialization value is 0, a only in main map * / Static Int A If you can, B is stored in one register, * / register int b = 0; / * Default storage class is auto * / int C = 0; / * The value of printing is 1, 0, 0 * / PrintF ("% d / n% D / N% D / N% D / N", I, A, B, C); other (); return;}

Void Other (Void) {/ * Global I Added to Pointer Variable * / Static Int * External_i = & I; / * i Redefined, Global I No longer visible * / int i = 16; / * a Only in Other functions Can be seen * / static int a = 2; A = 2; / * The value of printing is 16, 4, 1 * / printf ("% D / N% D / N% D / N", I, A, * EXTERNAL_I);

In this example, the definition of the variable I is in the outer layer, and there is initial value 1, and an extern in the main function will be used to illustrate one reference of the outer layer I. Static variable A default is 0, because the initialization statement is omitted. Call the PrintF printed value 1,0,0 and 0.

In the Other function, the address of the global variable i is used to initialize the Static pointer variable external_i. This work is because the global variable has a Static life, and its address does not change in program execution. Next, the variable I is redefined as a local variable and has initial 16. This redefine does not affect the value of the external layer I, it hides by the name of its local variable. The value of global variable I is now only indirectly accessed by pointer External_i in this block. Attempting to assign the address of the AUTO variable I to a pointer, it is not possible because it may not be the same at each block. The variable A is used as a Static variable and initializes 2. This will not conflict with the a conflict in main because the STATIC variables in the inner layer are only visible in the blocks they explain. Variable A increases 2, gives 4 as a result. If the Other function is called again in the same program, the initial value of A is 4. The internal STATIC variable exits in the program, and then re-enters the value of the set when their blocks are rendered.

Function Description of Storage Camell

You can use the Static or Extern storage class indicator in the function description. The function always has a global survival period.

Microsoft Special Office

The function description of the internal layer and the function of the external layer have the same meaning. This means that a function is visible from the desired point to the remainder of the converter, even in the local scope.

Microsoft End

The visibility rules of functions are a bit different from the visibility rules of the variable, as follows:

* Description STATIC is visible only in defining its source file. The function in the same source file can call the Static function, but in other source files cannot be accessed directly. You can use the same name to not conflictize another Static function in different source files.

* Visible in all source files illustrated by extern (unless you reintertain such a function to static). Any function can call a extern function.

* Omployed a function of the storage class indicating that the default is extern.

Microsoft Special Office

Microsoft allows redefining a extern identifier for static.

Microsoft End

indicator

The type indicator defines a variable or function description in the description.

grammar

Type indicator:

Void

charr

Short

int

Long

Float

Double

Signed

unsigned

Structure or joint indicator

Enumeration indicator

TypedEf name

Signed Char, Signed Int, Signed Short Int, and Signed long int types are called "integer" with their unsigned correspondence and ENUM. FLOAT, DOUBLE and long DOUBLE type indicators are called "floating point". You can use any integer or floating point in a variable or function description. If a type indicator is not provided in one instruction, use it as an int.

Optional Keywords Signed and Unsigned can be located or keep up with any integer, in addition to the ENUM, can be used as a type indicator alone, in which case they are respective as Signed Int and UNSIGNed Int. When used alone, the keyword int assumptions to signed. When used alone, the keyword LONG and SHORT are Long Int and Short Int.

Enumeration Types Consider the basic type. The type of enumerated type is discussed in the "Enumeration Description" behind this chapter.

There are three uses: indicating a function return value, pointing out a parameter type table of a function that does not accept parameters and pointers indicating a non-specified type. You can use the VOID type to explain the function of the return value or indicate a pointer that does not specify the type. For information on parentheses after VOID appears after a function name, see "Parameters" in Chapter 6 "Function".

Microsoft Special Office

Type detection is an ASI compatibility, which means that type short and int are different types. For example, redefine in the Microsoft C compiler is accepted by previous compiler versions.

INT myfunc ();

Short myfunc ();

The following example also produces a warning for indirect assignments between different types:

INT * PI;

Short * ps;

PS = Pi; / * Current warning * /

The Microsoft C compiler also produces a warning about different symbols, for example:

Signed Int * Pi;

UNSIGNED INT * PU;

PI = Pu; / * Now produces a warning * / type VOID expression evaluation is for side effects. You can't use (do not exist) a value of a type VOID expression, or you cannot convert a VOID expression (implicit or explicit conversion) to any type other than VOID.

If you need a different type of expression in the context that requires a VOID expression, its value is discarded.

In order to match the ANSI specifications, void ** cannot be used as int **. Only void * can be used as a pointer to an unspecified type.

Microsoft End

You can use TypeDef to create additional type indicators, as described in the "Typedef description" in this chapter. Information about each type of size.

Data type indicator and equivalent

This book usually uses the format of the type indicator listed in Table 3.1, not a long format, which assumes that the CHAR type default is symbol. Therefore, in this book, Char is equivalent to Signed Char.

Table 3.1 Type indicator and equivalents

Are equivalent type indicator signed char1charsigned intsigned, intsigned short intshort, signed shortsigned long intlong, signed longunsigned char-unsigned intunsignedunsigned short intunsigned shortunsigned long intunsigned longfloat-long double2-

1. When you make the char type defaults to unsigned (by specifying / j compile option), you can't say Signed Char as Char.

2. In the 32-bit operating system, the Microsoft C compiler maps long double to Double.

Microsoft Special Office

You can specify the / j compile option to change the default char type from a symbol to unsigned. When this option works, Char is the same as unsigned char, and you must use the Signed keyword to indicate a symbolic character value. If a char value shows that it is a symbol, the / j option does not affect it, and the value is symbolized when the width becomes the int type. When the width becomes into the ING type, the char type is 0 extension.

Microsoft End

Type modifier

Type modifiers give one of the two properties of an identifier. Const Type Modulation Description An object is not modified. Voatile Type Description A item, which can change when it is sometimes exceeded if it appears, for example, when performing threads.

Two type modifiers Const and Volatile have only one in one instruction. Type modifiers can be used with any type indicator, but they cannot appear after the first comma in multiple descriptions.

For example, the following instructions are legal:

Typedef volatile int vi;

Const int CI;

The following explanation is not legal:

TypedEf Int * I, Volatile * VI;

Float F, Const CF;

The type modifier is only related when accessing the identifier as a l value in the expression. See Chapter 4, "L V-Values ​​and R Value Expressions" in Chapter 4, "Expression and Assign".

grammar

Type modifier:

Const

Volatile

The following is a legal const and volatile description:

INT const * p_ci; / * Constant INT pointer * /

INT const (* p_ci), / * constant int 1 pointer * /

INT * const cp_i; / * int original pointer * /

INT (* const cp_i); / * intone constant pointer * /

Int volatile vint; / * volatile integer * /

If a specifications of an array type include type modifiers, modified is its element, not an array type. If a specification of a function type includes a modifier, its behavior is uncertain. None of the Volatile and Const do not affect the range of the object or the arithmetic properties.

Here lists how to use const and volatile:

* const keywords can be used to modify any basic or set types, or pointers for any type of objects or a TypedEF. If an item is used with a const type modifier, its type is Const Int. A Const variable can be initialized or placed in a read-only storage area. Const Keywords are useful for explaining that consocizers because this function does not change the pointer in any way. * The compiler assumes anywhere in the program, and a volatile variable can be accessed by using or modifying the unknown process of its value. Therefore, regardless of the optimization specified by the command line, you must generate each assignment code or a volatile variable reference code, and even its appearance does not work.

If you use Volatile alone, it is assumed to be INT. The Volatile Type Indicator is used to provide reliable access to a specific memory location. For data objects that can be accessed or changed by a signal processor, pass concurrent execution programs or an I / O control register mapped by a particular hardware such as a memory, or a change is used. You can declare a variable for the survival period to volatile, or a single reference to Volatile.

* A item can be const and volatile, in which case the item will not be reasonably modified by its own program, but can be modified by some asynchronous processes.

Description of the specifier and variable

The rest of this chapter describes the format and meaning of variable types, which are summarized in the following table. In particular, the following sections explain how to explain the following.

Variable type description

Simple variables have a single value variable for integer or floating point

Variables consisting of elements of the same type

The pointer points to the variables of other variables, contains the position of the variable, stores instead of address format

Enumerate variables have an integer, saving a value in a named integer constant collection

Structure variables consisting of collections with different types of values

Mixed variables consisting in several different types of values ​​in the same storage space

One specifier is a description that introduces the name in the program, which can include a modifier, such as a * (pointer), and any Microsoft call convention.

Microsoft Special Office

In the specifier:

__declspec (thread) char * var;

Char is the type indicator, _ _DECLSPEC and * are modifiers, var is the name of the identifier.

Microsoft End

You can use the description to specify the array of specified types, specify the pointer to the type value, and a function of the specified type return value. The specifier appears in the array and pointer descriptions are described later in this chapter.

grammar

Reference:

Pointer OPT direct specifier

Direct specifices:

Identifier

(Indicator)

Direct explanation [constant expression OPT]

Direct specifier (parameter type table)

Direct explanatory (identifier analysis OPT)

pointer:

* Type Modifier OPT

* Type Model Table OPT Pointer

Type modifier:

Type modifier

Type modifier table type modifier

Note: For a reference syntax of an explanatory, see the grammatical syntax in the "Description Overview" of this chapter or see Appendix A "C language syntax summary" behind this volume.

When a specifier is composed of an identifier of a non-decorator, the description has a basic type. If an asterisk (*) appears on the left side of an identifier, this type is modified to a pointer type. If the identifier is tightening to square brackets [], this type is modified to an array type. For more information on the priority interpretation, see "Interpreting More Complex Notes" later. Each specifier illustrates at least one identifier. A setup must include a fully described type indicator. This type indicator gives a type of an array type element, and the type of object refers to the object of the pointer type or a return value of a function.

Arranges and pointers are described in more detail later in this chapter. Several simple explanatory formats are described as follows:

INT List [20] / * Description A number of numbers of 20 integers * /

Char * cp; / * Description Pointer of a char value * /

Double Func (Void) / * Description A function named func, no parameters, return value is a double value * /

INT * APTR [10]; / * Description A array of 10 pointers * /

Microsoft Special Office

The Microsoft C compiler does not limit the number of indicators that modifies an arithmetic, structure, or joint type. This amount is limited only by the available memory space.

Microsoft End

Simple variable description

The description of the simple variable is the simplest format of the direct specifier, which indicates the name and type of the variable, which also points to the storage class and data type of the variable.

The storage class or type (or both) is required in the variable description, and there is no type of variable (eg, var;) a warning. grammar

Reference:

Pointer OPT direct specifier

Direct specifices:

Identifier

Identifier:

Non-numeric

Identifier

Identifier number

For arithmetic, structural, combined, enumeration and VOID types, and types named by typedef, you can use a simple specifier in one instruction because the type indicator provides all types of information. The pointers, arrays, and function types require more complex specifiers.

You can use an identifier table, with a comma between the identifiers to indicate several variables in the same description. All variables have the same basic type in this description. E.g:

INT X, Y; / * Description two int type simple variables * /

INT const z = 1; / * Description Type Int Variables * /

Variables X and Y For a special implementation to save any value defined by the INT type. Simple object z is initialized to 1 and is not modified. If the description of Z is an unexpected static variable or has a file range, it accepts an initial value 0 and cannot be modified. Unsigned long reply, flag; / * Description Two variables named Reply and Flag * /

In this example, both variables Reply and Flag have unsigned long types and saves unsigned integer values.

Enumeration description

An enumeration is made up of a named integer constant. An enumerated type description gives the name of the enumeration flag (optional) and defines a collection of named integer identifiers (called "enumeration", "enumeration", "enumerator" or "member "). A constant with an enumeration type stores one of the values ​​of the enumeration set of the type defined.

The ENUM type variable can be used in the indicator expression, as an operand, an operation of all arithmetic and relational operators, provides another method of replacing the #define preprocessor command, using it to generate values ​​and abide by normal range The advantages of the rules.

In ANSI C, the expression that defines a value of an enumerator always has an int type, so the storage associated with an enumeration variable is a storage required for a single int value. A value of an enumeration or an enumerated type can be used in C language to allow an integer expression anywhere anywhere.

grammar

ENUM indicator:

ENUM identifier Opt {enumerator table}

ENUM identifier

An optional identifier named enumeration type defined by the enumerator table. This identifier is often referred to as the "flag" of the enumeration specified by the table. A type of indicator is:

ENUM identifier {enumerator table}

Description This identifier is a flag of enumeration specified by an enumerator table. The enumerator table defines the "enumerator content". The enumerator table is described in detail below.

If an illustration of a flag is visible, subsequent use of this flag and omitting the description of the enumerator table indicates the type of enumerated enumeration. This flag must refer to a defined enumeration type, which must be in the current range. Because the enumeration type is defined elsewhere, the enumerator table does not appear in this description. The typefef description of the type of enumerated derived type and enumeration type can be used to use the enumeration flag before the enumeration type is defined.

grammar

Enumerator table:

Enumerator

Enumerator table, enumerator

Enumerator:

Enumeration

Enumeration of ordinary = constant expression enumeration:

Identifier

Name the value of the enumerator in each enumerator constant in the enumerator table. By default, the first enumeration is associated with the value 0, which is associated with the value of the next enumeration (a constant expression 1), unless you are explicitly associated with other values. An enumerated name is equivalent to its value.

You can use enumeration = constant = constant expression to override the default sequence of values. Therefore, if the enumeration is often in the enumerator table, the enumerator is associated with a value given by the constant expression. The constant expression must have an int type and can be negative.

The following rules apply to a member of an enumeration:

* A enumeration set can contain repetitive constant values. For example, you can use two different identifiers to zero, which is the same concentration, possibly Mull and Zero.

* The identifier in the enumeration set must be different from other identifiers having the same visibility in the same range, including normal variable names, and other enumeration tables.

* Enumeration markers Comply with general range rules, they must be different from other enumerations, structures, and joint marks of the same visibility.

example

These examples illustrate the description of the enumeration:

ENUM DAY / * Defines an enumeration type * /

{SATURDAY, / * Name DAY and describes the variables of this type Workday * / Sunday = 0, Monday, Tuesday, Wednesday, / * Wednesday, and 3 associations; default value 0 is associated with SaturDay. The identifier sunday is explicitly set to 0. The remaining identifier gives 1 to 5 values ​​according to the default. In the following example, a set of DAY is assigned to the variable Today:

ENUM day TODAY = Wednesday;

Note that the name of the enumeration is used as assignment. Since the DAY enumeration type is previously explained, only the enumeration flag DAY is necessary.

In order to explicitly assign an integer value to a variable of an enumerated data type, use a type shape:

Workday = (enum day) (days_VALUE-1);

This model is recommended in C, but it is not required.

ENUM BOOLEAN / * Description A rating data type called Boolean * /

{FALSE, / * false = 0, true = 1 * / true};

ENUM BOOLEAN END_FLAG, Match_Flag; / * Boolean Type of Two Variables * /

This description can also be specified as:

ENUM BOOLEAN {false, true} end_flag,

Match_flag; /

or

ENUM BOOLEAN {False, true} end_flag;

ENUM BOOLEAN MATCH_FLAG;

Examples of using these three variables are as follows:

IF (match_flag == false)

{.. / * Statement * /}

End_flag = true;

You can explain the unnamed enumerator data type. The name of the data type is omitted, but the variable can be explained. Variable Response is a variable of the type defined:

ENUM {yes, no} response;

Structural description

A "Structure Description" named a type and specifies a sequence of a "member" or domain) with different types of variable values ​​(called structure ". An optional identifier called "flag" gives the name of the structural type, which can be used in a reference to the structural type, and a variable of a structural type saves the entire sequence defined by this type. The structure is similar to the well-known "record" in other languages.

grammar

Structural or joint identifier:

Structural or combined identifier OPT {structure description table}

Structural or combined identifier structure or joint:

Struct

union

Structural description table:

Structural description

Structure Description Table Structure Description

The content of the structure is defined as:

Structural description:

Indicator modifier table

Structure Description Table Indicator Table:

Type indicator indicator modifier table OPT

Type modifier indicator modifier table OPT

Structural description table:

Structural specifier

Structural description table,

Structural specification structure

Reference:

Description of a structural type is not a structure to set additional space, it is just a module, which is convenient for later explanation of structural variables.

The previously defined identifier (flag) can be used to point to a structural type defined elsewhere. In this case, the structural instruction table cannot be repeated until it is defined. The description of the pointer of the structure and the type of typef of the structural type can be used before the struct type is defined. However, this structure definition must be encountered before the actual use of the size of its domain. This is incomplete definition of type and type logo. To accomplish this definition, a type definition must occur later in the same range.

Structure Description Table indicates the type and name of the structural member. A structural instructions include one or more variables or bitmaps.

Each variable described in the Structure Description Table is defined as one member of the structure type, and the variable description in the structural description table has the same format in the description of the other variables discussed in this chapter, except that the description cannot contain a storage class indicator or In addition to the initialization statement, the structural member can have any type, an incomplete type or a function type except the VOID.

One member cannot explain the type with the structure containing the member, but a member can explain the pointer to the structure of the member, which has a flag. This allows you to build a structural linked list.

Structure and other identifiers follow the same range rules. The structural identifier must be different from other structures, joints, and enumeration marks with the same visibility.

Each structure in the structural description table illustrates that in this table must be unique. However, the identifier name in a structural description table does not have to distinguish between the identifiers in other common variable names or other structural descriptions. The nested structure can also be accessed by the file-wide layer description. For example, give this description: Struct A

{INT X; struct b {int y} var2;} var1;

The following description is legal:

Struct a var3;

Struct b var 4;

example

These examples illustrate the structure of the structure:

Struct Employee / * Defines a structural variable named TEMP * /

{Char Name [20]; int ID; long class;} TEMP;

The Employee structure has three members, name, ID, and class. Name members are an array of 20 elements; IDs and CLASs are simple members of int and long types, respectively. Identifier Employee is a structural identifier.

Struct Employee Student, Faculty, STAFF

This example defines three structural variables: Student, Faculty, and STAFF. Each structure has three members of the same table. These members are illustrated as structural type EMPLOYEE, defined in the previous example:

Struct / * Defines an unnamed structure and a * / {

Float X, Y;

} Complex; / * Structure variable is named complex * /

The Complex structure has two Float type members x and y, which is not a flag, so it is not named or unknown. Struct Sample / * Defines a structure named X * /

{Char C; float * pf; struct sample * next;} x;

Two members at the beginning of this structure are a char variable and a float value pointer. The third member Next description is a pointer to the defined structure type (SAMPLE).

It is useful if the flag that does not require a name is useless. This is a case where explains all structural instances.

E.g:

Struct {Int x; int y;} mystruct;

The embedded structure is usually unnamed.

Struct SomeStruct {struct / * Non-named structure * / {INT X, Y;} Point; int type;} W;

Microsoft Special Office

The compiler allows a sizeless or 0-dimensional array as the last member of a structure, and if the size of a constant array is not in different cases, it is useful. One of this description is as follows:

Struct identifier {Description Set type array name [];

A sizeless array can only be used as the last member of a structure. The structure containing the unmetential array descriptions can be nested in other structures, and there is no more members in the structure containing its structure. The array of such structures is not allowed, and when the SizeOf operator is applied to this type of variable or this type itself, the size of the array is assumed to be 0.

When this structure is another structure or a combined member, no explanatory can also specify the structure. The domain name is raised to the structure including it. For example, a less command is as follows:

Struct s {float y; struct {Int a, b, c;}; char STR [10];} * p_s;

.p_s-> b = 100; / * Structure 1 聽聽 聽聽 聽聽 聽聽

For information on structural references, see "Structure and Joint Members" in Chapter 4, "Expression and Assignment".

Microsoft End

Locale

In addition to explaining a structure or a combined member, a structural specifese can also specify the number of positions, referred to as a bit domain, and its length is set by the colon after the domain name of the specifier. One bit domain is interpreted as an integer.

grammar

Structure specifier:

Specify

Type indicator specifier OPT: constant expression

This constant expression indicates the domain of the domain. The type indicator of the specifier must be unsigned int, signed int or int, and the constant expression must be a non-negative integer. If its value is 0, this explanation is not an explanatory. There is no enabled array, a bit field pointer, and a function of the return bit field. An optional indicator name is a domain. The bit field can only be described as part of a structure. Take the address operator (&) cannot be applied to the composition of the bit field.

The unnamed bit field cannot be referenced, and their content is unpredictable at runtime. They can be used as a "dummy" domain in order to assign value. A untrusted domain that is specified as 0 is assured that the member is kept with its members to store from an int boundary in the structural description table. The bit domain must also be long enough to include bit mode, for example, such two statements are illegal:

Short A: 17, / * illegal! * /

INT Long Y: 33 / * illegal! * /

The following example defines a two-dimensional structure array and named Screen:

Struct {UNSIGNED SHORT ICON: 8; UNSIGNED SHORT Color: 4; Unsigned Short underline: 1; Unsigned Short Blink: 1;}

Screen [25] [80];

The array contains 2000 elements, each element being a structure containing four bit domains: Icon, Color, underline, and Blink. The size of each structure is two bytes.

The bit domain has the same semantics with integer types. This means that a bit domain in an expression is identical to the same way as the same basic type variable, regardless of how many bits in the bit field.

Microsoft Special Office

Processing with INT definitions is symbolic. A Microsoft Expansion ANSI C standard allows the bit field to have a CHAR and LONG type (Signed and unsigned). Unnamed bitmines with basic types of long, short or char (signed or unsigned) are mandatory to borders suitable for this basic type.

Assign bit fields from a minimum effective digital bit to the highest valid numeric bit, in the following code:

Struct mybitfields {UNSIGNED SHORT A: 4; UNSIGNED SHORT B: 5; Unsigned Short C: 7;

Void main (void) {Test.a = 2; test.b = 31; test.c = 0;}

These positions are arranged as follows

00000001 11110010

CCCCCCB BBBBAAAA

Since the 8086 processor cluster stores the lower byte of the integer before the high byte, the upper integer 0x01f2 is stored in the physical memory first stores 0xF2, then 0x01.

Microsoft End

Structure storage and assignment

Microsoft Special Office

Structural members are stored in the order of their description: The first member has the lowest memory address, and the last member has the highest memory address.

Each data object has an alignment request. For the structure, this requirement is the maximum requirements of its members. Each object assigns an offset to:

Offset% alignment requirements == 0

If the integer type has the same size, and if the next bit field is suitable for the current allocation unit and through the unified cross-alignment requirements of the domain, the adjustment bit field is the same 1, 2 or 4 byte allocation. unit.

In order to save space or make the existing data structure, you can store the structure more compact or loose. / Zp [n] Compiler option and #pragma pack control structure data how to "compact" into the memory. When you use the / zp [n] option, here N is 1, 2, 4, 8 or 16, each structural member after the first structural member stores a byte boundary, which is a domain alignment or compact Size (n). It is very small here, indicating a formula, the byte boundary is:

MIN (N, SIZEOF (item))

The N is the compact size indicated by the / zp [n] option, and the item is a structural member. The default compact size is / zp8. In order to use the PACK compilation instruction to indicate the compact, the compact is a compactness referred to in the non-command line, gives a Pack-compilation indication, where the compact size is 1, 2, 4, 8 or 16 prior to this structure. In order to reinstall the compact in the command line, pointing out a Pack compile instruction without a parameter.

For the Microsoft C compiler, the bit domain defaults to the LONG size. Structural members are aligned in size or / zp [n] sizes, which is smaller. The default size is 4.

Microsoft End

Joint description

A "joint description" pointed out the collection of variables, which is optional, a flag named the union. This variable is called a combined member, which can have different types, which is similar to "variable record" in other languages.

grammar

Structure or joint indicator:

Structural or combined indicator OPT {structure description table}

Structural or combined identifier structure or joint:

Struct

UNION structure description table:

Structural description

Structure Description Table Structure Description

The joint content is defined as: Structure Description:

Indicator modifier table structure description table;

Indicator modifier table:

Type Indicator Indicator Modifics Table OP

Type modifier indicator modifier table OPT

Structural descriptions:

Structural specifier

Structural description table, structure specifier

Variables with UNION types Store one of the values ​​defined by this type. The same rule control structure and joint instructions. The combination can also have a domain.

A joint member cannot have an incomplete type, type VOID or function type. Therefore, members cannot be one example of the combination, but it can be a combination of the joint type.

A joint type Description is just a template, and the memory does not save until the variable is explained. Note: If a combination of two types is explained, the combination is unreliable with other types of access. For example, a combination of float and int, stores a FLOAT value, but it is used as an int to access it. In this case, this value depends on the internal storage of the FLOAT value. Its integer value is unreliable.

example

The following is a combination example

Union Sign / * A definition and a description * / {int svar; unsigned uvar;} number;

This example defines a joint with the SIGN type, indicating a variable named Number, which has two members:

Svar is a symbol integer and an unsigned integer UVAR. This note allows the current value of Number as a symbolic or unsigned value. The logo associated with this federation is Sign.

Union / * Defines a two-dimensional array * / {struct {unsigned int icon: 8; unsigned color: 4;} screen [25] [80];

The Screen array contains 2000 elements, each element of the array is a single joint with two members: Window1 and ScreenVal. WINDOW1 members are members of two bit domains: ICON and Color structure. Screenval members are an int. At any time, each federated member saves the structure of INT or Window1 represented by ScreenVal.

Microsoft Special Office

When a combination is another structure or a combined member, you can not name the nesting. The following is an example of a unknown union:

Struct str {Int a, b; union / * unknown federated * / {char C [4]; long L; float f;}; char c_Array [10];} my_str;

MY_STR.L == 0L; / * My_STR Union in the reference * /

The combination is often nested in one structure, which includes such a domain, which gives the type included in the combination at any particular time. Here is an example of a combination of a combination:

Struct x {int type_tag; union {INT X; float y;}}

See Chapter 4, "Structure and Joint Members" in Chapter 4, "Expression and Assignment".

Microsoft End

Combined storage

Storage associated with a combined variable is the store you need to make the biggest member in this union. When a smaller member is stored, the federated variable can contain unused memory space. All members are stored in the same memory and start on the same address.

Each time a value is assigned to different members, the original stored value is overwritten. E.g:

Union / * Defines a federated * / {char * a, b; float f [20];} x;

X The union member is in turn in turn according to the order described in order, a CHAR value, a char value, and an array of FLOAT values. X Allocation Storage is a storage required to have 20 elements, as F is the longest franchise.

Because the federation is not associated, its type is unnamed or "unnamed".

Array description

A "array description" named array and the type of specified elements, which also defines the number of elements in the array. Variables with array types are pointers of array element types.

grammar

Description

Description Indicator Initialization Description Table OPT;

Initialization Description:

Initialization specifier

Initialization Description Table, initialization specifier

Initialization specifier:

Specify

Reference = Initializer

Reference:

Pointer OPT direct specification

Reference:

Direct explanatory [constant expression OPT] Because constant expressions are optional, there are two formats in syntax:

* The first format defines an array variable. The constant expression parameters in square brackets indicate the number of elements in the array, which if there must be an integer type, and has a value greater than zero. Each element has a type given by a type indicator. It can be any type outside of non-Void. An array element cannot be a function type.

* The second format illustrates a variable defined elsewhere. It omits the constant expression in square brackets, but not square brackets. This format can only be used when the array is previously initialized and indicating that it is a parameter of a parameter or an array of an array in the program.

In both formats, direct specifiers are named the variables and modify the type of the variable. The square brackets ([]) after the specifier ([]) modify the specifier as an array type.

Type modifiers can be applied to an array type object, but the modifier is applied to an element instead of an array itself.

You can explain an array of array (a "multi-dimensional" array), format:

Type Indicator Description [constant expression] [constant expression] ...

Each constant expression in parentheses defines the number of elements of the given dimension: two-dimensional array has two enclosed expressions, three-dimensional array has three enclosed expressions, and the like. If you have initialized the array, it means that it is a parameter or a reference to an array of explicitly defined definitions in the program, then you can omit the first constant expression.

You can define various types of pointer arrays by using complex specifiers, as described in this chapter "Explain more complex specifiers".

Arranges are stored in line, for example, the following arrays consist of two lines, each row:

Char a [2] [3];

First store the three columns of the first line, then the three columns of the second line. This means that the last subscript changes faster.

In order to reference a single element of an array, a subscript expression is used, as described in the "Suffix Operator" in Chapter 4 "Expression and Assignment".

example

These examples illustrate the description of the array;

Float Matrix [10] [15];

This two-dimensional array is named Matrix, with 150 elements, each element has a float type.

Struct {float x, y;} complex [100];

It is an explanation of a structural array. This array has 100 elements, each element being a structure containing two members.

Extern char * name [];

This statement describes the type and name of the array pointing to char. The actual definition of Name appears elsewhere.

Microsoft Special Office

The type of integer needs to save a largest size of an array is size_t size, which is defined in the stddef.h header file. SIZE_T is a unsigned int, which is 0x00000000 to 0x7cffffff.

Microsoft End

Array storage

The store associated with an array type is a storage required for all of its elements. All elements of an array store from the first element to the last element in an adjacent manner and gradually increase the memory location.

Pointer

A "pointer description" named a pointer variable and specifies the type of object pointing to the variable. Save a memory address in a variable description of the pointer.

grammar

Reference:

Pointer OPT direct specifier

Direct specifices:

Identifier

(Indicator)

Direct explanation [constant expression OPT]

Direct specifier (parameter type table)

Direct explanatory (identifier analysis OPT)

pointer:

* Type Modifier OPT

* Type Model Table OPT Pointer

Type modifier:

Type modifier

Type modifier table type modifier

The type indicator gives the type of the object, which can be any basic, structural, or joint type. Pointer variables can also point to functions, arrays, and other functions (for information on explanation and interpretation of more complex pointer types, see "Explaining more complex specifiers" later in this chapter.

By making the type indicator to Void, you can post later the type of specification to the pointer. Such an item is written as a void * as a "VOID pointer". A variable that describes a pointer to Void can be used for pointers of any type of object. However, in order to perform more operations on or referred to in the pointer, for each operation, the type of type must explicitly specify (Type Char * and Type VOID * variables are assignd, without using type styling). Such transformations are consistent with one type shape (for more information, see "Type Model Conversion" in Chapter 4, "Expression and Assignment").

Type modifiers can be const or volatile, or both. They respectively indicate that the pointer cannot be modified by the program itself (const) or the pointer can be reasonably modified (VOLATILE) with a process of exceeding program control (see "Type Modifiers" in front of this chapter). The specifier name variable can include a type modifier. For example, if the specifier represents an array, the type of the pointer can be modified to a array of pointers.

You can illustrate a pointer for a structure, a combination, or enumerator type by explaining a structure, a joint or enumerator type. You can explain the pointer by using the structure or joint flag shown in the following example. This explanation is allowed because the editor does not need to know the structure or the size of the combined spatial, which is not required to know the pointer variable.

example

The description of the pointer is explained below:

Char * message; / *

Message pointer variable * /

The Message pointer points to the variable of the char type.

INT * POINTERS [10]; / * Description A pointer array * /

The Pointers array has 10 elements, each element being an int type variable pointer.

INT (* Pointer) [10]; / * Description Pointer with 10 elements * /

Pointer variable points to an array with 10 elements that have an int type.

INT const * x; / * Describe a pointer variable x, pointing to a constant value * /

The pointer X can be modified to point to different int values, but the value it points to cannot be modified.

const Int Some_Object = 5;

INT OTHER_Object = 37;

INT * const y = & fixed_object;

Const volatile * const z = & some_Object;

INT * Const Volatile W = & Some_Object;

The variable Y in this description is description as a constant pointer for an int value. It points to the value can be modified, but the pointer must always point to the same location: the address of the Fixed_Object. Similarly, Z is a constant pointer, but it also illustrates a pointer to an int, its value cannot be modified by the program. Another indicator volatile indicates that although the value referred to by Z cannot be modified by the program, it can be reasonably modified by the process that runs concurrently with the program. Description of W indicates that the program cannot change the value referring to, and the program cannot modify the pointer.

Struct List * Next, * Previous; / *

Using the logo of the list * / This example illustrates two pointer variables next and previous, pointing to the structural type list. This instructions can appear before the List structure type definition (see the next example), as long as the List type definition has the same visibility with the description.

Struct {char * token; int count; struct list * next,} line;

Variable Line has a struct type named named, and the List structure type has three members: the first is a pointer of the char value, the second is the int value, the third is a pointer to another List structure.

Struct id {unsigned int id_no; struct name * pname;} rord;

Variable RECORD has a structural type ID. Note PNAME illustrates another pointer to another structural type Name, which can appear before the Name type definition.

Address Storage

The number of storage quantities and addresses required by an address depends on the implementation of the compiler. Different types of pointers do not guarantee the same length. Therefore, sizeof (char *) does not have to be equal to SIZEOF (INT *).

Microsoft Special Office

For Microsoft C compilers, sizeof (char *) is equal to SIZEOF (INT *).

Base pointer

Microsoft Special Office

For Microsoft32-bit C compilers, a base pointer is a 32-bit offset from a 32-bit pointer base. The base address is useful for the segment of the exercise control allocation object, thereby reducing the length of the executable and increasing the execution speed. Typically, it is pointed out that a base pointer is:

Type__based (basis)

The change in the "base pointer" of the base site can make the specification of a pointer like a basis. The base pointer is an offset of the memory segment starting at the beginning of the pointer. The pointer based on the pointer is a 32-bit compilation in the format of the __based keyword. In such compilation, they are 32-bit displacement distances from one 32-bit base. One use of a pointer-based pointer is to include a persistent identifier of a pointer. A linked list consisting of a pointer based on a pointer can be stored in a disk, and then reloaded to another place to keep a valid pointer.

A pointer for a basic pointer is given as follows.

Void * vpbuffer; struct llist_t {void __based (vpbuffer) * vpdata; struct llist_t __based (vpbuffer) * llnexT;};

The pointer VPBuffer is assigned to the address of the memory allocated in a back point in the program. The linked sheet repositions relative to the VPBuffer value.

Microsoft End

Abstract specifier

A Abstract Declarator is a set of identifiers that consists of one or more pointers, arguments, or function modifiers. The pointer modifier (*) is always located in front of the identifier in one set; the array ([]) and functions (()) modifier follow the identifier. Knowing this, you can determine that the identifier is currently in the location in an abstract setup and explains the setup accordingly. For additional information and examples of complex indicators, see the next section "Explain more complex specifiers". The generic typedef can be used to simplify the specifier, see "typedef description" behind this chapter.

The abstract specifier can be complex, parentheses in a complex abstract specifier indicate a particular explanation, as in the description they do for complex specifiers.

These examples describe the abstract specifier:

INT * / * Type name of an int type pointer * /

INT * [3] / * array of three int pointers * /

INT (*) [5] / * Pointer in the fifth INT * /

INT * () / * There is no parameter and returns the function of the INT's pointer * /

/ * Pointer with a function of a pointer without parameters and returns INT * /

Int (*) (void)

/ * An array of unidentified elements, its element is a pointer of such a function, each function has a unsigned Int

Parameters and other parameters that are not specified and returned to INT * /

Int (* count []) (unsigned int, ...)

Note: The abstract specifier is not allowed by an empty bracket () because it is blurred. It is impossible to determine if the implied identifier belongs to parentheses (which is a type that cannot be modified) or whether it is in parentheses (which is a function type).

Explain more complex specifiers

You can include any specifier in parentheses to point out a special explanation of a "complex specifier". A complex indicator is an identifier modified by a plurality of arrays, pointers, or function modifiers. You can apply a combination of various arrays, pointers, and function modifiers to a single identifier. Typically, TypeDef can be used to simplify the description. See "Typedef Description" later in this chapter.

In the interpretation of the complex specifier, square brackets and parentheses (the modifier of the identifier right) is more preferred than an asterisk (which is the modifier on the left of the identifier). Square brackets and parentheses have the same priority and bind to left to right. After the specifier is fully explained, the last application type indicator is applied. By using parentheses, you can cover the default binding order and force a specific explanation. However, the identifier name does not use parentheses, which may be mistaken as a parameter table.

The simple way to explain the complex indicator is to use the following four steps "from the outside" read:

1. From the identifier, look for the square brackets and parentheses (if any) on the right.

2. Explain these square brackets or parentheses, then look for the asterisk on the right.

3. If you encounter a right round bracket at any time, return to the content application rules 1 and rules 2 for the parentheses in front.

4. Apply the type indicator.

Char * (* (* var) ()) [10];

^ ^ ^ ^ ^ ^ ^ ^

7 6 4 2 1 3 5

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!

In this example, the steps are marked in order and explained as follows:

1. Identifier Var Description

2. A pointer

3. A function returns

4. A pointer

5. A group with 10 elements

6. A pointer

7. CHAR Value Example The following example illustrates how other complex instructions and how parentheses affect the meaning of a description.

INT * var [5]; / * Pointing an array of int values ​​*

/ An array modifier has a higher priority than the pointer modifiers, so the VAR illustrates an array. The pointer modifier is applied to the type of the array element; therefore, the array element is a pointer to the INT value.

INT (* var) [5]; / * INT value of the array pointer * /

In this VAR note, the pointer modifier given by parentheses is higher than the priority of the array modifier, and Var illustrates a pointer to the number of 5 INT values.

Long * Var (long, long); / * Returns the function of the long pointer * /

The priority of the function modifier is also higher than the level of the fingers. So this VAR is used to illustrate that VAR is a function that returns a pointer for a long value. This function illustrates two long values ​​as a parameter.

Long (* var) (long, long), / * Returns a pointer to the function of the LONG * /

This example is similar to one of the previous, and the pointer modifier given by parentheses has a higher priority than the function modifier, and VAR is illustrated as a pointer to a function that returns a LONG value. Again, this function has two long parameters.

Struct Both / * Function Pointer * / {/ * Return Structure * / INT A; Char B;} (* Var [5]) (Struct Both, Struct Both);

An array element cannot be a function, but this document proves how to explain the array of function pointers. In this example, VAR illustrates an array of five pointers of a function that returns a structure with two members. The parameter of this function illustrates two structures having the same structural type BOTH. Note that the parentheses enclosed in * var [5] is necessary, without them, this note is illegal, trying to illustrate an array of functions, as follows:

/*Unlawful*/

Struch Both * Var [5] (Struct Both, Struch Both);

The following statement shows a pointer array:

Unsigned int * (* const * name [5] [10]) (void);

The Name array has 50 elements, tissue in the form of a multi-dimensional array, its element is a pointer of a pointer, which is a constant, which points to a function, the function does not have a parameter and returns a pointer without a symbolic type.

The next example is a function that returns a pointer to an array with 3 Double values:

Double (* var (double (*) [3])) [3];

In this note, the function returns a pointer of an array because the function of the returned array is illegal. Here VAR illustrates a function that returns a pointer to an array with three Double values. This function var has a parameter that returns a value, which is a pointer with three Double values. The parameter type is given by the abstract setup.

The parentheses outside the parameter type in the parameter is needed, without them, the parameter type is an array of three Double values. See "Abstract Description" in front of this chapter on the discussion and example of abstract statement.

Union Sign / * Pointer Array * / {/ * United Pointer * / INT X; Unsigned Y;} ** var [5] [5];

This example illustrates how to place parentheses to change the meaning of the description. In this example, VAR is a 5-element array of a fellow pointer of the combined pointer. See "Typedef Description" for how to avoid complex instructions using typedef.

initialization

A "initializer" is a sequence that is assigned to the value of the variable. You can set the initial value of a variable by applying one initializer in the variable description to the specifier. Assign the value of the value or the value of the initializer to the variable.

The following sections describe how to initialize scales, collection, and string types. "Scale Type" includes all arithmetic types plus pointers. "Collection Type" includes arrays, structures, and union.

Initialization scale type

When the initialization scale type, the value of the assignment is assigned to the variable. Application assignment conversion rules (see Chapter 4, "Type Conversion") for conversion rules.

grammar

Description:

Description Indicator Initialization Description Table OPT;

Description Indicator:

Storage class indicator Description Of OPT

Type Indicator Description Indicator OPT

Type modifier description indicator OPT

Initialization Description:

Initialization specifier

Initialization instruction table, initialization specifier

Initialization specifier:

Specify

Description = Initializer / * For the initialization of the scale * /

Initializer:

Assignment Expressions You can initialize any type of variable, to follow the following rules:

* The variables illustrated in the file range layer can be initialized. If you don't explicitly initialize the variables of the external layer, it is default to 0. * A constant expression can be used to initialize any global variable description with Static storage class indicator, which is illustrated as a STATIC is initialized in program execution. If you don't explicitly initialize a global static variable, it is default to 0, and each member with a pointer type assigns an empty pointer.

* The variable description description by the AUTO or REGISTER storage class indicator is initialized when the control is transmitted to the block of the variable. If you omit a initializer in an Auto or Register variable description, the initialization value of the variable is uncertain. For automatic and register values, the initializer is not limited to a constant, which can be any expression that contains previously defined values, or even function calls.

* The initial value of the external variable and the initial value of all static variables, whether externally or internal, must be a constant expression (for more information, see "constant expressions" in Chapter 4). Since any external description or the address of the static variable is constant, it can be used to initialize an internal description of the STATIC pointer variable. However, the address of the AUTO variable cannot be used for a static initializer because each execution of the block is

It may have different values. You can initialize Auto and Register variables using constants or variables.

* If an illustration of an identifier has a block range, and the identifier has an external connection, the description cannot be initialized. Example The following example illustrates initialization:

INT x = 10

Integer variable x is initialized to constant expressions 10.

Register int * px = 0;

The pointer PX is initialized to 0 to generate a "empty" pointer.

Const Int C = (3 * 1024);

This example uses a constant expression (3 * 1024) to initialize C as a constant value that cannot be modified because there is a const key.

INT * b = & x;

This statement initializes the pointer B with the address of another variable x.

INT * Const a = & z;

The pointer A is initialized with the address of the variable z. Since it is const, the variable A can only initialize and cannot be modified. It always points to the same location.

INT Global; INT FUNCTION (VOID) {INT local; static int * lp = & local; / * illegal initialization * / static int * gp = & global; / * legal initialization * / register int * rp = & local; / * legal initialization * /}

Global variables Global in the external layer, so it has global survival. The local variable Local has an Auto store, which is only an address during the execution of its function. Therefore, it is not allowed to initialize the Static pointer variable LP with the address of LOCAL. The Static pointer variable GP can initialize the address of Global because the address is always the same. Similarly, * RP can be initialized because RP is a local variable, has a very quite initializer, each time you enter the block, local has a new address, then assign it to RP.

An initial set type

A "collection" type is a structure, a combination, or an array type. If a collection type contains a member of the collection type, you can recursively apply initialization rules.

grammar

Initializer:

{Initializer Table} / * For set initialization * /

{Initializer table,}

Initializer table:

Initializer

Initializer table, initializer

The initializer table is a table with a comma-separated initializer that each initializer is a constant expression or an initializer table. Therefore, the initializer is nested. This format is useful for initializing a collection member of a collection type, as explained by the following section. But if an initializer of an automatic identifier is a single expression, it does not need to be a constant expression, and the assignment of the identifier is only required.

For each initializer table, the value of the constant expression is assigned to the corresponding member of the collective variable.

If the value of the initializer table is less than a collection type, the remaining members of the set type are initialized to 0. An automatic identifier is not explicitly initialized, and its initial value is uncertain. If the value of the initializer table is more than a collection type, an error is generated. These rules are applied to each embedded initializer table, as well as the entire collection.

An initializer of a structure is a table of the same type of expression or an initializer containing a member of the currency ({}). Unnamed bitmasterists do not initialize. When a combination is initialized, the initializer table must be a single constant expression. The value of this constant expression assumes the first member of the United.

If an array has an unknown size, a member of the initializer determines the size of the array whose type is also complete. There is no method in c, indicating the repetition of an initializer, or initializes an element in an array without providing its front value. If you need this operation in the program, write this routine in the assembly language. Note: Members of the initializer can set the size of the array:

int x [] = {0, 1, 2}

But if you point out the size and the number of initializer, the compiler generates an error.

Microsoft Special Office

The maximum size of an array is defined by size_t, which is defined in the stddef.h header file. SIZE_T is a unsigned int value from the 0x00000000000000000000 to 0x7cffffffffffffffff.

Microsoft End

example

This example gives an initializer of an array:

INT P [4] [3] =

{

{1, 1, 1},

{2, 2, 2},

{3, 3, 3,},

{4, 4, 4,},

}

This statement indicates that the element of a 4 × 3 array and initializes its first row of 1, and the element of the second line is 2, so, etc., until the fourth line. Note that the initializer table of the third line and the fourth line contains a comma after the last constant expression. The final initializer table ({4, 4, 4,},) also has a comma, which is allowed but does not need. Separation constant expressions require only a comma, and the separation initializer table only needs a comma.

If the set member does not embed the initializer, the value simply assigns each element of the child collection. Therefore, the initialization in the previous example is equivalent to the following statement:

INT P [4] [3] =

{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4

}

The single initializer in the initializer table can be enclosed with a parentheses, which helps to clarify the above examples. When you initialize a collection variable, you have to carefully use the curd and initializer table. The following example explains the interpretation of the compiler on the curb parentheses in more detail.

TYPEDEF STRUCT {INT N1, N2, N3;} Triplet; Triplet NList [2] [3] = {{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, / * Row 1 * / {{10, 11, 12}, {13, 14, 15}, {16, 17, 18}} / * line 2 * /};

In this example, NList illustrates an array of 2 × 3 structures, each structure has three members, and the first line of the initializer is to NLIST, as follows:

1. The first left flower bracket of line 1 indicates that the compiler starts to initialize the first collection member of NLIST (that is, NList [0]).

2. The second left flower parentheses indicates the first set member of the start to initialize NLIST [0] (that is, the structure at NLIST [0] [0]).

3. The first right ribbon termination structure NList [0] [0] is initialized, the next left flower bracket begins NLIST [0] [1].

4. The process continues until the end, the closed right brackets terminate the initialization of NLIST [0]. The line 2 assigns the value to the second line of NLIST in the same manner. Note that the outermost curly brackets of the row 1 and row 2 enclosed initializer are needed. The outer curly brackets are omitted below, and an error is generated when constructing:

Triplet nlist [2] [3] = / * This causes a bug * /

{

{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, / * line 1 * /

{10, 11, 12}, {13, 14, 15}, {16, 17, 18} / * line 2 * /

}

In this configuration, the first left flower brackets of the line 1 began to initialize NList [0], which is an array of three structures.

Value 1, 2 and 3 give three members of the first structure. When the next right flowers are encountered, the initialization of NLIST [0] is completed, and the two remaining structures in the three-structure array are automatically initialized to 0, similarly, {4, 5, 6} initialization. The first structure in the second row of NLIST. The remaining two structures of NList [1] are set to 0. When the compiler encounters the next initializer table ({7, 8, 9}), it tries to initialize NLIST [2]. Since NLIST is only two lines, this trial causes an error.

In the following example, the three int members of X initialize 1, 2 and 3, respectively.

Struct List {INT I, J, K; Float M [2] [3];} x = {1, 2, 3, {4.0, 4.0, 4.0}}

In the above LIST structure, three elements in the first row of M initialize 4.0; downlink element default initialization is 0.0. Union {char x [2] [3]; INT I, J, K;} y = {{{'1'}, {'4'}}};

In this example, the combined variable y is initialized. The union of the first element is an array, so the initializer is a set initializer. The initializer table {'1'} assigns the value to the first line of the array. Since there is only one value in this table, the elements in the first column are initialized to character 1, and the remaining two elements in the row are initialized to zero. Similarly, the first element of the second line of X

It is initialized to character 4, and the two elements of the row is initialized in the first element.

Initialization string

You can initialize a character (or width character) array with a string text (or wide string text), for example:

CHAR CODE [] = "ABC";

Initializing Code is an array of 4-element characters, the fourth element is empty, to terminate all string text.

A identifier table can only be equal to the number of initialized identifiers. If you specify an array size less than the length of the string, the extra characters are ignored. For example, an array of initialized codes is a three-element:

CHAR CODE [3] = "abcd";

Only three characters of the beginning of the initializer to Code. Character D and space characters ending at this string are discarded. Note that a non-end string is created (that is, there is no 0 value flag ends) and generate a diagnostic message indicates this condition.

Description:

Char s [] = "abc",

T [3] = "abc"; equivalent: char s [] = {'a', 'b', 'c', '/ 0'},

T [3] = {'a', 'b', 'c'};

If the string is shorter than the specified array size, the subsequent element is initialized to 0.

Microsoft Special Office

In Microsoft C, the length of string text can reach 2048 bytes.

Microsoft End

Basic type of storage

Table 3.2 summarizes the storage associated with each basic type.

Table 3.2 Basic type size

Type Storage Char, Unsigned Char, Signed Char1 byte Short, Unsigned Short2 Byte Int, Unsigned INT4 Byte Long, Unsigned Long4 Byte Float4 Byte Double8 Byte Long Double8 byte

The C data type is a general directory. "Integer" type includes char, int, short, long, signed, unsigned, and enum. The "floating point" type includes Float, Double, and Long Double. The "arithmetic" type includes all floating point and integer type.

Type char

The char type is used to store an integer value of a member representing a character set. This integer value is the ASCII code corresponding to the specified character.

Microsoft Special Office

A unsigned char character value has a range from 0 to 0xFF hexadecimal values. A Signed Char has the range from 0x80 to 0x7f. These ranges are converted from 0 to 255 decimal and -128 to 127 decimal. Compiler Options / J Change The default value changes from Signed to unsigned.

Microsoft End

Type INT

A size with a symbol or unsigned int item is a standard size of an integer on a special machine, for example, in the 16-bit operating system, the int type is usually 16-bit or 2 bytes. In the 32-bit operating system, the int type is usually 32-bit or 4 bytes. Therefore, the INT type is equivalent to the Unsigned Short or the unsigned long type based on the SHORT INT or LONG INT type, the unsigned int type is equivalent to the SHORT INT or LONG INT type. The int type represents the value of a symbol, unless otherwise specified.

Type Specify INT and Unsigned Int (or unsigned) Define some feature of the C language (eg, ENUM type). In these cases, the definition of special implementation INT and UNSIGNED INT determines the actual storage.

Microsoft Special Office

The symbolic integer is expressed in 2 complement format. For negative numbers, the highest bit saves symbols 1, for positive and 0, it is 0. The range of values ​​is given in Table 1.3, they come from the Limits.h header file. Microsoft End

Note: The int and unsigned int types are widely used in the C program because they allow a specific machine to handle integer values ​​in their most effective way. However, since the size of the int and unsigned int types is changed, programs depend on a specific INT size cannot be ported to other machines. In order to make the program more portability, you can use a SizeOF operator expression instead of hard code data size (as discussed in "SIZEOF operator" in Chapter 4 "Expression and Assignment"). Types with dimensions

Microsoft Special Office

Microsoft specializes in supporting an integer type of band size. You can use the _ _intn type indicator to describe 8, 16, 32 or 64-bit integer variables, where n is the size of the integer variable, in place. The value of n can be 8, 16, 32 or 64. The following example illustrates four types of size:

__INT8 nsmall; // Description 8-bit integers __

INT16 NMEDIUM; / / Description 16 integers

__INT32 NLARGE; / / Description 32-bit integers_

_INT64 nhuge; // Description 64-bit integers

The start of three strip-size integers is a synonym with the same size, which is useful for writing multiplay cross-transplantation. Note __int8 data type and char type is synonym, __ int16 and short types are synonyms,

_ _INT32 and int types are synonymous. __INT64 Types There is no equivalent ANSI corresponding section.

Microsoft End

Float type

The floating point number uses the IEEE (Electronic Engineer Association) format, the single-precision floating point type has 4 bytes, by a symbolic bit, one 8-bit exceeding 127 binary index and a 23-digit end. The tail indicates a number of 1.0 to 2.0. Since the high end of the mantissa is always 1, it is not stored in this number. This means that the floating point type is similar from 3.4e-38 to 3.4e 38. You can explain that a variable is a floating point number or a double precision. The difference between these two types is that they can represent that they need to store the active bits and their range. Table 3.3 illustrates the differences in the significant digits and storage requirements.

Table 3.3 Floating point type

Type effective digital byte number of floating points 6-74 double precision 15-168

The floating point variable indicates a tail, which contains the value of the value and the index, and the index contains the size of the value. Table 3.4 illustrates the number of bits assigned to the tail and index each floating point type. The most important bit of any floating point or double precision is always symbolic, if it is 1, the value is considered to be negative; otherwise, the value is considered positive.

Table 3.4 The length of the index and the tail

Type Index Length Length Floating Point 8 23-bit Double Accuracy 11 Bit 52 Bit

Because the index is stored in unsigned format, the index is biased with half of its possible values. For floating point types, it is 127; for the double precision type, the bias is 1023. You can calculate the actual index value by subtracting the offset value from the index value.

The mantissa is stored in a binary fraction greater than or equal to 1 and less than 2. For floating point and double-precision type, a boot 1 is implied in the most important position in the mantissa, so the tail is actually 24 and 53 long, even if the most important bit is not stored in the memory.

Instead of the storage method described above, the floating-point package stores binary floating point values ​​in reverse-specific value, "reverse-specific value" is a non-0 floating point value with the reserved index value, the most important bit of the mandate Yes 0. Using reverse specifications format, a floating point value can be expanded in precision. You can't control a floating point to represent a regular format or a reverse-specific format. Floating-Point Package does not use reverse-specific format, unless the index is less than the minimum in the format of the spectrum.

Table 3.5 gives the minimum and maximum values ​​that can be stored in each floating point type variable. The values ​​listed in this table are applied only to the specified floating point value. The reverse-specific floating point value has a smaller minimum. Note that the value reserved in the 80x87 register is always represented by an 80-bit specified format. The value can only be represented by a reverse-specific format only when storing 32-bit or 64-bit floating point variables (Variables for the float type and long type).

Table 3.5 Range of floating point type

Type minimum maximum floating point 1.175494351E-3831.402823466E 38 double precision 2.2250738585072014E-3081.7976931348623158E 308

If the accuracy is not very important, consider using floating point types for floating point variables; the opposite If accuracy is the most important standard, use double precision type.

Floating point variables can be upgraded to a greatest effective number of bits (from floating point types to double precision type). This improvement often occurs when you perform arithmetic operations on floating point variables. The arithmetic operation always takes the highest precision as the high precision of the variable. For example, consider the following description:

FLOAT F_SHORT;

Double f_long;

Long Double F_Longer;

F_short = f_short * f_long;

In the above example, the variable F_Short is lifted into a double precision type and multiplied by F_long. The result is that the result is rounding the floating point type before assigning F_Short.

In the following example (described in the previous example), the arithmetic operation is performed on the floating point (32-bit) precision of the variable. Then increase the results to double precision type:

f_longer = f_short * f_short;

Double type has 8 bytes of double-precision values ​​with Double types. This format is similar to the Float format, except that there is 11 bits of more than 1023 index and a 52-bit tail number plus implicit high end 1. This format gives a Double type approximation range from 1.7e-308 to 1.7e 308.

Microsoft Special Office

The Double type contains 64-bit: 1 symbol bit, 11 index bits, and 52 tail digits. It ranges to /- 1.7e308, at least 15 numbers accurate.

Microsoft End

Long Double type

The value of a variable is the minimum value and maximum value in which the number is indicated. However, because C is discussed in detail in "Type Conversion" in Chapter 4, "Expression and Assignment"), you cannot always use the maximum or minimum for a particular type of constant in the expression.

For example, constant expression -32768 is applied by an arithmetic load operator (-) to constitute of constant 32768, since 32768 is too large to be represented as a short INT number, which is given to a LONG type. As a result, constant expression -32768 is a long type.

You can only prove to Short Int -32768 through the type shape. There is no information discard in the type shape, since -32768 can be inside internally by one byte. In the decimal representation, the value 65000 is a symbolic constant, which is given to the LONG type because 65000 is not suitable for Short. Such 65,000 values ​​can only be enforced to the UnsignedShort type or given this value by a type shape or given to an octal or hexadecimal representation, or it specifies that it is 65000U to represent the Unsigned Short type. You can make this value for this value for the unsigned short type without losing information, because as an unsigned value, 65000 is adapted to represent in 2 bytes.

Microsoft special

Long Double contains 80 bits: 1 bit represents symbol, 15 bits represents an index, and 64 bits indicate the mantissa.

It ranges from /- 1.2e4932, which is at least 19 numbers. Although Long Double and Double are separate types, long Double and Double representations are consistent.

Microsoft End

Incomplete type

An incomplete type is to describe an identifier and lack the type of information required to determine the identifier size. A "incomplete type" can be:

* A type of structure that does not point out its members.

* One is not pointed out of the joint type of its members.

* A number of arrays that do not point out dimensions. The Void type is an incomplete type that cannot be completed. In order to complete an incomplete type, it is pointed out that the lack of information, as described below shows how to establish and complete incomplete types.

* In order to establish an incomplete structural type, a structural type does not point a member. In the following example, the PS pointer points to a incomplete configuration type called Student:

Struct student * ps;

* In order to complete an incomplete structure type, the same structural types are used in the same range within the same range:

Struct student

{

Int unm;

} / * Student structure is now complete * /

* In order to establish an incomplete array type, an array type is described without specifying its repeated count, for example:

Char a []; / * a has incomplete type *

/ * In order to complete an incomplete array type, the same name is used in the same range within the same range:

Char a [25]; / * a now has a complete type * /

Typedef description

A TypedEf illustrates an instructions for using typedef as a storage class. This explanatory becomes a new type. You can use TypeDef to explain the type of C has defined or a shorter or more meaningful name that you have explained. The TypeDef name allows you to make a detailed implementation of the package.

A Typedef description is the same as the interpretation of the variable or function description, but the identifier replaces the assumption type specified by the description, becoming synonyms of this type.

grammar

Description:

Description Indicator Initialization Description Table OPT

Description Indicator:

Storage class indicator Description Of OPT

Type Indicator Description Indicator OPT

Type modifier description indicator OPT

Storage class indicator:

TypeDef Type Indicator:

Void

charr

Short

int

Long

Float

Double

Signed

unsigned

Structure or joint indicator

Enumeration indicator

TypedEf Name TypeDef Name:

Identifier

Note that a Typedef explains that the type cannot be established, which establishes a synonym in an existing type or otherwise specify the name of the type. When a TypeDef name is used as a type indicator, it can be combined with some types of indicators, but it cannot be other. Acceptable modifiers include Const and Volatile.

The TypeDef name shares a namespace with the normal identifier (for more information, see "Name Space" in Chapter 2, "Program Structure"), and therefore, a program can have a TypeDef name and a local range identifier of the same name. E.g:

Typedef char flagtype;

INT main () {} int myproc (int) {int flagtype;}

When a local range identifier is the same name as TypedEf, or when a structure or a combined member will be described in the same range or more inner layer, the type indicator must be indicated. This constraint is illustrated as follows:

Typedef char flagtype;

Const flagtype x;

In order to be an identifier, a structural member or a joint member reuses the FlagType name, must provide its type:

Const int flagtype; / * Requires type indicator * 0 /

It is not enough as follows:

Const flagtype; / * incomplete specifications * /

Because FlagType is part of this type, it is not an identifier. The following description is an illegal description:

Int; / * illegal explanation * /

You can use Typedef to explain any type, including pointers, functions, and array types. You can explain the TypeDef name of the structure or a federal pointer before defining a structure or a federated type, and the definition has the same visibility with the description.

The TypeDef name can be used to improve the readability of the code. All the three Signal descriptions of the following are precisely indicated by the same type, and the beginning is not using any TypedEf name:

TypedEf void fv (int), (* pfv) (int); / * typedef description *

/ VOID (* Signal (int)) (int)) (int)) (INT);

FV * Signal (int, fv *); / *

Use typedef type * / PFV signal (int, pfv); / * Using TypeDef Type * / Example Description Typedef Description: typedef int whole; / * Description WHOLE INT synonyms * / Note What can now be used in variables In the description, such as WHOL I; or Const WHOLEI; but explains long whole i; it is illegal.

Typedef struct club {char name [30]; int size, year;} group;

This statement shows that Group is a structural type with three members. Because a structural flag CLUB, the TypeDef name (Group), or the structure flag can be used in the description, you must use the structure keyword with the flag, and you cannot use the TypeDef name to use the structure keyword.

Typedf group * pg; / * Use the previous TypeDef name description 1 pointer * /

Type PG illustrates a pointer for Group type, which is defined as a structural type.

Typedef void Drawf (int, int);

This example provides a DrawF type, which is a function, no return value, two int parapers. For example, description: Drawf Box; equivalent to the description: Void Box (int, int);

Expansive storage class properties

Microsoft Special Office

The expanded attribute syntax is simplified and the standardized C language Microsoft Special Displays. Storage class properties using the extended properties include Thread, Naked, Dllimport, and DLLEXPORT.

The expansion attribute syntax of a particular storage class information uses the __declspec keyword, which indicates an instance of a given type, which is stored in Microsoft Special Storage Class Properties (Thread, Naked, Dllimport, or DLlexPort). Examples of other storage modifiers include static and extern keywords, but these keywords are the ANSI C part, so they cannot be covered by the expanded attribute syntax.

grammar

Storage class indicator:

__DECLSPEC (Extension Description Modifier Sequence) / *

Microsoft Special Office * /

Expansion Description Modifier Sequence:

Expansion Description Modifier OPT

Expansion Description Modifier Sequence

Expansion Description Decorator Extension Description Modifier:

Thread

naked

DllImport

Dllexport

Use a blank separated modifier. Note that the expansion of the modifier sequence can be empty, in this case, __ declspec has no effect.

Thread, Naked, Dllimport, and DLLEXPORT storage class properties are only the characteristics of their applications or functions; they cannot redefine the type properties of the function itself. The thread attribute only affects the data. Naked properties only affect the function. DllImport and DLlexPort properties affect function and data.

Microsoft class is over

DLL input and output

Microsoft Special Office

Dllimport and DLlexPort Storage Class modifiers are C language Microsoft Special Displays. These modifiers define the DLL client interface (expandable file or additional DLL). See "DLL input and output functions" in Chapter 6, "Function" in Chapter 6, "DLL input and output functions" in Chapter 6, "Function".

Microsoft End

Naked

Microsoft Special Office

Naked storage class properties is the special point of the C language Microsoft. The compiler generates a code that does not have preambularly and ends with the function described with the Naked storage class property. When you need to write your own preface / end-part code sequence, the NAKED function is useful when you need to write your own preface.

Naked functions are useful for writing a virtual device driver.

See "Naked Functions" in Chapter 6, "Functions" in Chapter 6, "Naked Functions" in Chapter 6, "Naked" in Chapter 6. Microsoft End

4thread partial storage

Microsoft Special Office

THREAD Local Storage (TLS) is a mechanism for assigning specific thread data in a given multi-thread process. In a standard multi-threaded program, the data is shared in all threads of a given process, and the thread local storage is the mechanism for assigning each thread data. For a complete discussion of threads, see "Processes and Threads" in the "Platform SDK" document.

The Microsoft C language includes an extended storage class properties, Thread uses a thread local variable with the __declspec keyword. For example, the following code illustrates an integer thread local variable and initializes it with a value:

_ _Declspec (thread) INT TLS_I = 1;

These guidelines must be followed when you explain the statically constrained threads.

* _ _Declspec (thread) can be interacted with delay loading of DDL input.

* You can only apply the Thread property to data instructions and definitions. It cannot be used for functions or definitions.

For example, the following code produces a compiler error:

#define thread __declspec (thread) thread void func (); / * Error * /

* You can only specify the Thread property on the data item with a static storage period. This includes global data (STATIC and EXTERN) and local static data. Automatic data cannot be explained with thread attributes. For example, the following code generates a compiler error;

#define thread __declspec (thread) Void Funcl () {thread Int TLS_i; / * Error * /} int func2 (thread INT TLS_i); / * Error * / {Return TLS_i;} l You must specify the thread local data and Define the use of thread properties, regardless of this description and definition, in the same file or separate file, for example, the following code generates an error:

#define thread __declspec (thread)

EXTERN INT TLS_I; / * This creates an error, because * /

INT THREAD TLS_I; / * Description and Definitions Did not the same * /

* You cannot use the Thread property as a type modifier. For example, the following code generates a compiler error: char * ch_ _declspec (thread); / * Error * / L The address of a thread local variable cannot be used as a constant, involving any expression of the address cannot be used as a constant expression. This means you can't use the address of a thread local variable as a initializer of a pointer. For example, the following code of the compiler flag has an error:

#define thread __declspec (thread)

Thread Int TLS_i; INT * P = & TLS_i; / * Error * /

* C allows for initialization of a variable with expressions involving its own reference, but only objects of non-static ranges, such as:

#define thread __declspec (thread)

Thread Int TLS_I = TLS_i; / * Error * /

INT J = J; / * Error * /

Thread Int TLS_I = SizeOf (TLS_i) / * Correct * /

Note that a SIZEOF expression of an initialized variable does not constitute a reference to itself, so it is allowed.

* __Declspec (thread) can be interactive with the delayed delay input from the DLL. For more information on using thread properties, see "Multi-threaded theme" in the online "Microsoft Visual C 6.0 Programmer Guide"

Microsoft End

转载请注明原文地址:https://www.9cbs.com/read-46766.html

New Post(0)