Basic Structure Knowledge of C language Review

Source: Internet
Author: User
Tags scalar
I. Basic Knowledge

1. The aggregate data type can store more than one individual data simultaneously. C provides two types of aggregate data types, arrays and structures.

(1) An array is a set of elements of the same type. Each element of an array is selected through subscript reference or indirect access by a pointer.

(2) The structure is also a set of values. These values are called its members, but each member of a structure may have different types.

2. array elements can be accessed by subscript, because the element length of the array is the same.

3. Because the members of a structure may have different lengths, they cannot be accessed using small labels. On the contrary, each structure member has its own name, which is accessed by name.

4. The structure is not an array of its own members. Unlike the array name, a structure variable is not replaced with a pointer when used in an expression. Structure Variables cannot use subscript to select specific members.

5. The Structure Variables belong to the scalar type. They can also be used as parameters passed to the function. They can also be returned from the function as return values. The structure variables of the same type can be assigned values to each other.

6. You can declare a pointer to a structure, get the address of a structure variable, or declare a structure array.

Ii. Structure Declaration

1. When declaring a structure, all its members must be listed. The list includes the type and name of each member.

Eg:

StructTag {

Member-list;

} Variable-list;

Struct Declaration consists of three parts: Tag, member-list, and variable-list. All optional parts cannot be omitted-they must contain at least two.

1> example:

Struct {

Int;

Char B;

Float C;

} X;

This statement creates a variable named X, which contains three members: an integer, a character, and a floating point number.

Struct {

Int;

Char B;

Float C;

} Y [20], * z;

This statement creates Y and Z. Y is an array containing 20 structures. Z is a pointer pointing to the structure of this type.

2> note:

The above two declarations are treated by the compiler as two completely different types, even if their member lists are identical. Therefore, the types of the variables Y and Z are different from those of X, so the following statement is used.

Z = & X; illegal

3> but does this mean that all structures of a specific type must be created using a separate declaration. Otherwise, the tag field allows a name for the member list.

Eg:

Struct simple {

Int;

Char B;

Float C;

};

This statement associates the simple tag with the member list. The Declaration does not provide a list of variables, so it does not create any variables.

2. A tag identifies a pattern used to declare future variables, but neither the tag nor the pattern itself is a variable.

Eg struct simple X;

Struct simple y [20], * z;

These declarations use tags to create variables. They are created in the same way as in the previous example. The difference is that X, Y, and Z are the same type of structure variables.

2. Another good technique that can be used to declare a structure is to use typedef to create a new type.

Typedef struct {

Int;

Char B;

Float C;

} Simple;

This technique works almost the same as creating a structure tag. The difference is that simple is now a type name rather than a structure label, so subsequent declarations may be as follows:

Simple X;

Simple y [20], * z;

Note: If you want to use the same type of structure in multiple source files, you should put the label declaration or typedef declaration in a header file. When the source file needs to use this declaration, you can use the # include command to include this header file.

3. structure member

1> the structure member can be any variable. The structure member can be a scalar, array, pointer, or other structure.

2> the name of a structure member can be the same as that of other structures. There is no conflict.

3. Access to structure members

1. Direct Access to structure members

The Structure Variable members are accessed through the dot operator symbol. The vertex operator accepts two operands. The left operand is the name of the structure variable, and the right operand is the name of the member to be accessed. The result of this expression is the specified member.

2. indirect access to struct members

If you have a pointer to the structure, we use the-> operator (arrow operator), like the dot operator. The Arrow operator performs indirect access to the left operator to obtain the structure pointed to by the pointer, then, like the dot operator, select a specified structure member based on the right operand.

3. structure self-Reference

Is it legal to include a member of the structure itself in a structure?

Eg:

Struct self_ref1 {

Int;

Struct self_ref1 B;

Int C;

};

This type of application is invalid because member B is a complete structure and will also contain Member B. The 2nd members are in another complete structure and will also contain its own member B. This will never end.

1> the following method is valid.

Struct self_ref2 {

Int;

Struct self_ref2 * B;

Int C;

};

This statement differs from the previous statement that B is a pointer rather than a structure. The compiler knows the length of the pointer before determining the length of the structure. Therefore, this type of auto-reference is valid.

2> the following is an incorrect usage:

Typedef struct {

Int;

Self_ref3 * B;

Int C;

} Self_ref3

The purpose of this declaration is to create the type name self_ref3 for this structure. However, it is incorrect, and the type name is not defined until the end of the declaration, so it is not defined in the structure declaration.

Use a structure label to declare B, as shown below:

Typedef struct self_ref3_tag {

Int;

Struct self_ref3_tag * B;

Int C;

} Self_ref3;

4. incomplete statement

Sometimes, you must declare structures that depend on each other. That is, one structure contains one or more members of another structure. Like a self-reference, at least one structure must exist in another struct in the form of a pointer. The problem lies in the declaration part: if each structure references tags of other structures, which structure should be declared first?

1> an incomplete statement is used to solve the problem. It declares an identifier as a structure tag. Then, use this label in the statement that does not need to know the length of the structure, for example, to declare a pointer to this structure. The following statement associates the tag with the member list.

2> In the following example, two different types of structures have a pointer pointing to another structure.

Struct B;

Struct {

Struct B * partner;

/* Other declarations */

};

Struct B {

Struct a * partner;

/* Other declarations */

};

In the member list, the incomplete declaration of Label B is required. Once a is declared, the member list of B can also be declared.

Four-structure Initialization

1. The structure initialization method is similar to the array initialization method. A comma-separated list of initial values located inside a pair of curly braces can be used to initialize each member of the structure. These values are written in the order of the structure member list. If the value of the initial list is not enough, the remaining structure members will use the default value for initialization.

2. If the structure contains arrays or structure members, the initialization method is similar to that of multi-dimensional arrays. A complete list of aggregate type members can be nested in the structure's Initial Value List.

Eg:

Struct init_ex {

Int;

Short B [10];

Simple C;

} X = {

10;

{1, 2, 3, 4, 5 },

{25, 'x', 1.9}

};

Related Article

Contact Us

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

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

A Free Trial That Lets You Build Big!

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

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

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