List, pointer, and reference of level 2 C ++ test site analysis

Source: Internet
Author: User

Array 4.1
Concepts of Test Site 1 Array
An array is a derived data type that uses a name to identify a group of ordered and identical data types. It occupies a continuous memory space. The characteristic of an array is (1) array name; (2) Types of elements in the array; (3) dimension (number of subscripts required to identify the array element); (4) array size (to accommodate the number of array elements ).
TIPS:
Before using an array, you must use a declaration statement to specify the preceding four features of the array.
Test site 2 one-dimensional array
1. Definition of one-dimensional array
A one-dimensional array is also called a vector. It is an array consisting of an array element with a lower mark. Its definition form is as follows:
Type array name [size];
Square brackets are subscript operators with the highest priority and right-to-left combination.
2. initialize a one-dimensional array
 
Like other basic data types, an array can also be initialized to each element of the array while being defined. The initialization expression is written in a pair of curly brackets in sequence. The array elements in braces are separated by commas (,).
. The size of the array is not specified during initialization. The Compiler determines the size of the Array Based on the initialization list. However, when only some elements are given for initialization, the array size must be specified.
TIPS:
Only after some elements are initialized, the initialization value of other elements is implicitly 0.
3. Access array elements
The syntax format for accessing array elements is:
<Array name> "<expression> 〕
The <expression> is a non-negative integer expression, that is, the subscript of the array. The array subscript is used to specify the element position in the array to be accessed. Note: The array subscript starts from 0.
Test site 3 two-dimensional array
A two-dimensional array is also called a second-level vector. we can regard a two-dimensional array as a one-dimensional array whose elements are one-dimensional arrays.
The general format for defining two-dimensional arrays is:
<Data type> <array name> [m] [N];
Each element in a two-dimensional array must be represented by two subscripts. The first one is the row subscript, and the last one is the column subscript. Therefore, [m] indicates the row base size of the two-dimensional array, and [N] indicates the base size of the column of the Two-dimensional array.
The expression of two-dimensional array elements is as follows:
<Array name> [<subscript 1>] [<subscript 2>];
The value ranges from 0 to 1.
Like a one-dimensional array, a two-dimensional array can be initialized while being defined, and the method is similar. Example:
Int A [2] [2] = }};
However, if initial values are assigned to all elements, one-dimensional arrays can be automatically counted based on the number of initial values to determine the size of the array, therefore, the size of the first dimension is not specified during definition, but the size of the second dimension cannot be omitted.
TIPS:
In C ++, the order in which two-dimensional arrays are stored in the computer is to arrange row-ordered storage, that is, to store 1st rows first, then 2nd rows, and so on.
Test Site 4 Multi-dimensional array
 
The definitions of one-dimensional and two-dimensional arrays can be inferred that multi-dimensional arrays are multi-level vectors. Their elements must be identified by adding N subscripts to the array name. The size of a multi-dimensional array is the product of dimensions. If the multi-dimensional array is used as a function
The size of the first dimension is not specified, but the size of other dimensions must be specified. Multi-dimensional arrays can only be passed to functions as reference parameters, and the return values of functions cannot be arrays.
Test site 5-character array
1. Concept of character array
 
A character array is an array of character types. Each element contains one character. Character arrays are also called strings. C ++ specifies that the last element of the character array must be '/0 '. For example, the number of characters
Group storage string: "Hello! World. "statement format: Char STR [] =" Hello! World "; but the system will automatically add a blank word at the end
'/0 '. That is to say, the character array stores a string one more byte than the actually used string.
2. Common string functions
C ++ provides a series of string processing functions that are included in the cstring header file. Common C ++ string processing functions are as follows.
(1) strcat string 1, string 2 ). This function is a String concatenation function that connects two strings. The specific method is to connect string 2 to the end of string 1 and store the result in string 1. Note: Make sure that the space of the array storing the result string is large enough.
TIPS:
After the two strings are connected, the character '/0' at the end of the previous array disappears.
(2) strcpy (string 1, string 2 ). This function is a character copy function that copies the characters in a string to another string variable. The specific method is to copy the characters in string 2 to string 1. Note: Make sure that the space of string 1 that stores the result is large enough.
 
(3) strcmp (string 1, string 2 ). This function is a string comparison function used to compare strings. The comparison method is as follows: if two strings are equal (matched ),
0 ("false"); If String 1 is greater than string 2 in the dictionary order, a positive number is returned. If character l is smaller than string 2 in the dictionary order, a negative number is returned.
(4) strlen (string ). This function is a string length function, which is used to evaluate the length of a string. The value of the function is the number of characters not including '/0' in the string.
(5) strstr (string 1, string 2 ). This function is a string lookup function. It is used to search for substrings in a string. The search method is to search for string 2 from the left in string 1. If the search is successful, return the position where string 2 first appears in string 1, return NULL. If String 2 is "", returns string 1.

4.2 pointer
Test site 6 pointer and address
A pointer is a very important derived data type that can be used to construct a complex data structure. A variable with a pointer type is called a pointer variable. The information stored by a pointer variable is the address of an object in the memory. You can use pointer variables to indirectly access objects. The general format of pointer variable declaration is:
<Data type> * <variable name>;
<Data type> indicates the object type of the pointer. In C ++, the pointer can point to any c ++ type. <variable name> is the pointer variable name; * must be placed before the variable name when defining the pointer variable.
*
And & are the two Special operators used by pointers. They are all single-object operators, both of which have a higher priority and a combination from the right to the left. & Is the bitwise operator. A pointer can be used with the same
Class Object address value initialization, with the & operator can get an object address in memory. * It is an indirect reference operator that is inverse to the address fetch operator. It obtains the object Value and requires its
The operation object is a pointer.
Before using any pointer variable, you must first assign it a memory address value that is valid for a specific object.
TIPS:
A pointer can also be initialized with another pointer of the same type; a pointer variable can also be assigned a null value.
Test site 7 pointer operation
A pointer is a type of data that must have a signed integer value. Because of the characteristics of the address itself, it also imposes some limitations on Pointer operations. Generally, there are four types of operations allowed by pointers.
(1) Value assignment. You can assign the address value of the variable pointed to by a pointer to it, or assign the address value of an array or the entry address value of a function in the memory to the corresponding pointer, you can also assign a pointer that has been assigned a value to another pointer of the same type.
(2) add and subtract the pointer and integer. The pointer and integer are added and subtracted, indicating that the pointer moves down and up in the same memory space. The unit of movement is its type length. This operation also includes the addition of 1 or subtraction of 1, that is, the pointer moves down or up a data type space pointed.
(3) under certain conditions, two pointers can be subtracted. For example, two pointers pointing to different elements of the same array can be subtracted, and the difference is the number of elements separated between the two pointers.
(4) under certain conditions, the two pointers can be compared. For example, two pointers pointing to elements in the same array can be compared. When the two pointers are equal, the two pointers point to the same array element.
Test site 8 pointer and array
In C ++, the relationship between arrays and pointers is extremely close. When processing arrays, the compiler often processes them in the form of pointers, so that array elements can be in the form of subscripts and pointers. These two forms are linked by the address relationship of the array. In C ++, the array name is a pointer. For a one-dimensional array, It is a level-1 pointer; for a multi-dimensional array, It is a multi-level pointer. For example:
Int A [4]; // A is a one-dimensional array name, which has four int Variables
* (A + I) and a [I] (where I =, 2, 3) are the same in Pointer representation.
The array name of a two-dimensional array is usually the row pointer of the array, and the next level pointer is called the column pointer. For example:
Int B [2] [4]; // B is a two-dimensional array name, which has eight int Variables
When using the pointer method, * (B + I) + J) and B [I] [J] (where I = 0, L; j =, 2, 3) is the same. If you use a pointer to represent the rows and columns of a two-dimensional array, you can obtain the following format:
* (B + I) + J)
In C ++, A String constant can be considered as an array of characters without a name. The C ++ compiler automatically allocates a space for it to store this constant, the value of a String constant is a pointer of 1st characters to the character array without a name. Its type is a character pointer.
4.3 reference
Concepts referenced in Test Site 9
A reference is generally considered as an alias for a variable. When a reference is created, the program initializes it with another variable or object name. The referenced changes are actually changes to the target. The reference is used to create a function parameter or return value. The reference format is:
<Type> & <reference Name> (<variable name>); or <type> & <reference name >=< variable name>;
TIPS:
The quote operator and the address operator are the same character but have different meanings.
Generally, the following rules should be observed when using references.
(1) The reference must be initialized at definition (the pointer can be initialized at any time ).
(2) The referenced operation is the operation on the referenced variable.
(3) assign a referenced address value to a pointer, And the Pointer Points to the referenced variable.
(4) You can assign a reference to a variable. The value of this variable is the value of the referenced variable.
(5) The reference cannot be used as an alias for other variables once initialized (the pointer can be changed to another variable at any time ).
(6) null references are not allowed.
4.4 Dynamic Storage Allocation
Implementation of Dynamic Storage Allocation in test site 10
1. Use new to obtain dynamic memory space
C ++
The new and delete operators provide the ability to dynamically allocate memory. The new function is to dynamically allocate space to program entities. New is a single object operator. The basic way to dynamically allocate buckets is
Declare a pointer to point to the space allocated with new that can accommodate a given type of data. The syntax for Dynamic Allocation of certain types of variables using new is as follows:
<Pointer> = new <type> ;,

Example: int * P = new int;
New can be initialized while dynamically allocating memory space for simple variables. Syntax format:
<Pointer> = new <type> (<initial value expression> );
Example: int * P = new int (256); // assign an int area to the pointer P and initialize the storage area.
It is 256.
New can be used to dynamically allocate arrays. Syntax format:
<Pointer> = new <type> [<number of elements>];
Example: int * P = new int [4]; // allocate four int-type spaces to address p.
TIPS:
New Cannot initialize the dynamically allocated array storage area.
2. Use Delete to release dynamic memory space
Delete is also a single object operator. The delete function is to reclaim the space dynamically allocated by the new operator.
Note: The dynamic storage space allocated by the new C ++ program must be released by Delete. That is to say, if the program needs to allocate dynamic memory space, new and delete always appear in pairs. The syntax for releasing a single variable dynamically allocated using Delete is as follows:
Delete <pointer>;
The syntax for releasing dynamically allocated Arrays Using Delete is as follows:
Delete [] <pointer>;
Example: Delete [] P; // release the array C pointed to by P, without the need to mark the space size.

 

Source: http://www.51edu.com/it/2008/1214/article_8659.html

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.