Structural part in MSDN

zhaozj2021-02-16  164

Typedef

TYPEDEF Type - Declare synonym;

The TypeDef keyword defines the synonym of the type-declared specified. Type - Declaration Identifier is another name (ie alias) of the type (ie, alias), the role of alias is the replacement type name when naming the instance of this type. You can also use the TypeDef assignment in the function definition.

Typedef introduced a name that declared in the Decl-Specifiers section within the TypeDef declaration to be used as synonymous characters as the type. The TypeDef declare does not introduce new types than in Class, Struct, Union, and Enum, but introduced an existing type of new name.

EXAMPLE

// Use the typedef keyword example

Typedef unsigned long ulong;

Ulong ul; // is equivalent to "unsigned long UL;"

Typedef struct mystructtag

{

INT I;

Float f;

Char C;

} mystruct;

MyStruct MS; // Equivalent to "Struct MyStructTAG MS;"

Typedef int (* funcptr) (); // funcptr is "pointing to the return value

// Synonym of the pointer of the integer

Funcptr Table [10]; // Equivalent to "INT (* Table [10]) ();"

Struct

Struct [Tag] {member - list} [declared];

[struct] tag declaration;

The role of the struct keyword is to define a structural type and / or a structural type variable. For more information, see the variable group part of anonymous structure and structure.

The structure type is a user-defined composite type. It consists of different types of "domain" or "members".

In C , structures and classes are the same, but by default, members of the structure are public.

Use structure

In C language, a structural body must be declared using the struct keyword. In C , once the type is defined, it is not necessary to use the Struct key.

When declaring structural type variables, structural type members are placed in a pair of curly braces, and the members are separated by semicolons.

For information, please see the Class, Union, and ENUM sections.

EXAMPLE 1

Struct Person // Declare Person Structural Type

{

INT Age; // Declare member type

Long SS;

Float weight;

CHAR Name [25];

} family_member; // Declaration Person type object

Struct Person Sister; // C Style Declaration Structure

Person Brother; // C style declared structural body

Sister.age = 13; / / Assignment for structural members

Brother.age = 7;

The variable can be initialized in a declared structure variable, and the initialized value is placed in a pair of curly brackets.

EXAMPLE 2

Struct Point // Declare Point Structure

{

INT X; // Define two members of X and Y

Int Y;

} Spot = {20, 40}; // Variable Variables of members in Spot

// x = 20, y = 40

Struct Point there; // Variable there is Point Type

Struct Cell // Declare the Cell Word Festival

{

Unsigned character: 8; // 00000000 ???????? Unsigned Foreground: 3; // 00000 ??? 00000000

Unsigned intensity: 1; // 0000? 000 00000000

Unsigned background: 3; // 0 ??? 0000000000

Unsigned blink: 1; // 0000000 00000000

} Screen [25] [80]; // Byte domain

Anonymous structure

Microsoft Details ->

An extension of Microsoft C is to allow the user to declare another structure in a structure, and the structure that is declared is not named. This nested structure is called anonymous structure. C does not allow anonymous structures.

You can access members in anonymous structures like access to members in the structure of an anonymous structure.

Microsoft detail end

EXAMPLE

// Anonymous structure example

Struct Phone

{

INT area;

Long Number;

}

Struct Person

{

CHAR Name [30];

CHAR SEX;

Int agec;

Int weight;

Struct phone; // anonymous structure; no name

} JIM;

Jim.Number = 1234567;

Variable number of structures in structures

Microsoft Details ->

Microsoft Extensions allows the last member of the C or C structure or class to be a variable size array. This array is called a variable group. The final variable group of structures allows to increase variable length strings or other arrays, thus avoiding overhead of execution pointer destruction at runtime.

Struct Person

{

Unsigned number;

Char name []; / / variable group

}

If the SIZEOF operator is used for this structure, the last array member size is considered to be 0. The array is 2 bytes, and this size is also the size of an unidentified member. To get the size of the Person type variable, you need to separateize the size of the array.

The size of the structure is obtained in the total size of the allocation according to the magnitude size. After allocating space, the array is copied into the array member of the structure.

Struct Person * PTR;

Char WHO [40];

Printf ("Enter Name:");

Gets (WHO);

/ / For structural, name and termination NULL allocation space

PTR = Malloc (SIZEOF (Struct Person) Strlen (WHO) 1);

// Copy string to Name member

STRCPY (PTR-> Name, WHO);

Once the variable group in the initialization structure, the size returned by the SIZEOF operator will not contain the size of the array.

Structural bodies containing variable groups can be initialized, but the structural array cannot be initialized.

Struct Person Me = {6, "Me"}; // Equity Logic

Struct Person you = {7, "you"};

Struct Person US [2] = {{8, "THEM"}, // incorrect

{9, "We"}};

The character array will obtain the space of the NULL after initialization of the string; the character array initialized by multiple independent characters (for example: {'a', 'b', 'c'}) will not terminate NULL.

A structure containing a variable group can occur in another structure, as a declaration as a declaration of the last member of the structure containing its structure. Classs or structures containing variable groups do not have direct or indirect virtual basis. For related information, please see the volatile and #define section.

Microsoft detail end

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

New Post(0)