C Primer Plus (14th)

Source: Internet
Author: User

14.1 create a structure statement

Structure declaration is the main method used to describe how a structure is combined. The statement is as follows:
Struct book {
Char title [MAXTITL];
Char author [MAXAUTL];
Float value;
};
This statement describes a structure consisting of an array of two characters and a float variable. It does not create an actual data object, but describes the elements that constitute such objects.
It can be declared as follows: struct book library; it declares the library as a structure variable designed using the book structure.
The members in the structure can be any type of C data structure, or even other structures.
This section describes the end Of the structure design definition with a semicolon after curly braces.
The tag name is optional.

14.2 define Structure Variables

The struct book library compiler creates a variable library. The compiler uses the book template to allocate space for this variable: a char array with MAXTITIL elements. These buckets are combined by a name library.
Struct book works like int and float.

14.2.1 initialization structure

Struct book library = {"the pirate", "rense vio", 1.95 };
In short, an initialization project is initialized using a comma-separated initialization project enclosed in curly brackets.
If the initial structure has a static storage period, the value in the initialization project list must be a constant expression.

14.2.2 access structure member

The structure member operator vertex (.) can be used to access the structure member. For example, library. value.

Initialize a specified project in the 14.2.3 Structure

C99 supports the specified initialization project of the structure. Its syntax is similar to that of the specified initialization project of the array.
For example, struct book library = {. value = 10.99}; and the specified sequence can be arbitrary.
In addition, the last value assigned to a specific member is the actually obtained value.

14.3 structured Array

When an array containing a large number of structures is used, it may be because the array is an automatic storage class object that places the information in the stack and requires a large bucket, A running error may be caused by a small default size of stack space used by the compiler.
To meet the requirements, you can use the compiler to set the stack size to 10000 or set the array to static or external (so that the array is not placed in the stack ).

14.3.1 declared structure array

Declaring a structure array is the same as declaring any other types of arrays.
Struct book library [MAXBKS];
This statement declares that the library is an array with MAXBKS elements. Each element of the array is a book-type structure. Therefore, library. [num] is a book structure.
Library itself is not a structure name, it is an array name of the element type struct book structure.

14.3.2 identifies a member of the structure array

Add a vertex operator after the structure name, followed by the member name. For example, library [0]. value;
A classic judgment condition while (count <MAXBKS & gets (library [count]. title )! = NULL & library [count]. title [0]! = '\ 0 ')
Note that the following information is entered: 12.50 [enter] This statement transfers the following Character Sequence 12.50 \ n
So add while (getchar ()! = '\ N') continue; clears the input line

14.4 nested Structure

Stuct names {char first [LEN]; char last [LEN];};
Struct guy {struct names handle; char job [LEN]; float income ;};
Struct guy fellow = {"Ewen", "mongoard"}, "salmon", 58112.00 };
Access to the nested structure fellow. handle. first.

14.5 pointer to structure

14.5.1 declaration and initialization structure pointer

Struct guy * him;
If barney is a guy-type structure, it can be like him = & barney;

14.5.2 access members using pointers


2. If he = & barney, then * him = barney, you can use (* him). income

14.6 transmit structure information to Functions

14.6.1 transfer structure member

As long as the structure member is a data type with a single value, it can be passed as a parameter to a function that accepts this specific type.
If you want the called function to affect the Member values in the called function, you can pass the Member address.
You can also define the function prototype double sum (const struct guy * me), which is called by sum (& barney.
You can also use double sum (struct guy me); To call it by sum (barney.

14.6.2 other structural features

Currently, C allows a structure to be assigned to another structure, which cannot be used for arrays.
The structure can be passed to a function as a parameter or return a function as a return value.

14.6.3 structure, or a pointer to the structure

The advantage of using a pointer as a parameter is that the pointer can be compatible with the C version of the old version, and the speed block must be switched to only one single address. The disadvantage is the lack of data protection. However, you can solve this problem by specifying the const.
The advantage of using the structure as a parameter is that the function processes copies of the original data, which ensures data security and the program style is clear. The disadvantage is that early C may not be able to handle it, and it will waste time and space.

 14.6.4 use character arrays or character pointers in the Structure

Because the structure uses pointers but is not initialized, other addresses may be overwritten in future use.
Therefore, if you need a structure to store strings, use character array members.

 14.6.5 structure, pointer, and malloc ()

A meaningful example of using pointers to process strings in the structure is to use malloc () to allocate memory and use pointers to store addresses.
Struct namect {char * fname; char * lname; int letters ;};
Void getinfo (struct namect * pst ){
Char temp [81];
Gets (temp );
Pts-> fname = (char *) malloc (strlen (temp) + 1 );
Strcpy (pts-> fname, temp );
Gets (temp );
Pts-> lname = (char *) malloc (strlen (temp) + 1 );
Strcpy (pts-> lname, temp );
}

14.6.6 compound text and structure

The text-compliant feature added by C99 is not only applicable to arrays, but also to structures.
For example (struct book) {"Hello", "you", 6.99}

14.6.7 telescopic array member (C99)

Rules for declaring a telescopic array member:
1. The members of the telescopic array must be the last group member.
2. The structure must contain at least one other member.
3. the telescopic array is declared as a normal array, except that it is empty in square brackets.
For example:
Struct flex
{
Int count;
Double average;
Double scores [];
};
You cannot use scores to do anything because it does not allocate any memory space. The intention of C99 is to allow you to use malloc () to allocate enough space to store the regular content of the struct flex structure and any extra space required by the telescopic array members.
For example, if you want to use scores to represent an array containing five duoble values, you can use struct flex * pf; pf = malloc (sizeof (struct flex) + 5 * sizeof (double ));

14.6.8 functions using structure Arrays

For example, double sum (const struct funds money [], int n); struct funds jones [2];
You can use sum (jones, N); or sum (& jones [0], N );

14.7 joint Introduction

Union is a data type that can store different types of data in the same bucket. For example
Union hold {int degit; double bigfl; char letter ;};
You can initialize a union. Because the Union stores only one value, the initialization rules are different from the structure's preliminary test.
1. You can initialize one consortium to another consortium of the same type;
2. The first element of the Union can be initialized;
3. According to the C99 standard, you can use a specified initialization project;
Only one value can be stored at the same time, and two values cannot be saved at the same time even if there is enough space.

14.8 Enumeration type

You can use the enumerated type declaration to represent the symbol name of an integer constant. Use the keyword enum to create a new type.
Enum spectrum {red, orange, yeelow, green, blue, violet}; enum sepcturm color; color = blue;
Enumeration constants are of the int type. Some enumeration attributes of C cannot be extended to C ++. For example, C allows the operator ++ to be used for enumeration variables, but C ++ does not.

In the above example, when % d is used to output red and orange, red = 0, orange = 1;, for example, array declaration and switch statement.
By default, the constants in the enumeration list are 0, 1, and 2. You can also specify the Declaration enum feline {cat, lynx = 10, puma, tiger}. The default cat value is 0, and lynx is 10, if no value is assigned to the constant, it is assigned to the subsequent value. Puma and tiger are respectively 11,12;

14.9 introduction to typedef

1. Unlike # define, the symbol name given by typedef is limited to the pair type rather than the pair value.
2. typedef is interpreted by the compiler rather than the Preprocessor.
Iii. typedef is more flexible than # define within a certain range
Example: typedef unsigned char BYTE; BYTE x, y [10], * z;

14.10 functions and pointers

A pointer can point to a function, which can be used as a parameter of another function to tell which function is used by the second function.
The function also has an address. the pointer to the function saves the address at the beginning of the function code.
Second, when declaring a function pointer, you must declare the type of the function to which it points. For example, void (* pf) (char *)
Pf is a pointer to a function. (* pf) is a function and uses (char *) as the parameter list of the function. void is the return type.
Void Toupper (char *); you can use pf = Toupper instead of pf = Toupper ();
Use * pf (mis) to apply the Toupper function to mis.
It can also be used as a function parameter, such as void show (void (* fp) (char *), char * str)

 

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.