From

xiaoxiao2021-03-06  112

Word Source VC World - C language classroom

Self-archiving, also dedicated to the same Delphi programmer as me

Description and use of array pointer variables

The pointer variable pointing to the array is called the array pointer variable. An array is composed of a continuous memory unit. The array name is the first address of this continuous memory unit. An array is also composed of each array element (subscript variable). Each array element has several consecutive memory cells in its type. The first address of an array element refers to the first address of several memory cells it occupies. A pointer variable can be directed to an array or pointing to an array element that imparts the address of the array name or the first element. To make the pointer variable point to the first address of the I element to give it or give it an array name.

The general form of array pointer variable description is: Type Design * Pointer Variable Name

After introducing the pointer variable, you can use two ways to access the array element. The first method is the subscript method, that is, the array element is accessed in the form of a [i]. This method is used when the array occurs in Chapter 4. The second method is a pointer method, i.e., in the form of * (PA I), using indirect access to the array element. Main () {Int A [5], I, * Pa; PA = a; for (i = 0; I <5; I ) {* PA = I; PA ;} PA = a; for (i = 0; I <5; i ) {Printf ("a [% D] =% D / N", I, * PA); PA ;}}

Next, an example is also given, the example is the same as the above, but the implementation is different. Main () {Int a [5], I, * PA = a; for (i = 0; I <5;) {* PA = I; Printf ("a [% d] =% d / n", i , * PA );}}

Array name and array pointer variable function parameters

The number of group names is the first address of the array. The actual parameter gates are actually the address of the transfer array, and the shape is also directed to the same array. Similarly, the value of the pointer variable is also the address, the value of the array pointer variable is the first address of the array, and of course, as a function of the function. FLOAT AVER (Float * Pa); main () {float sco [5], av, * sp; int 1; sp = sco; printf ("/ ninput 5 score: / n"); for (i = 0; i <5; i ) scanf ("% f", & sco [i]); av = AVER (SP); Printf ("Average Score IS% 5.2F", AV);} Float Aver (FLOAT * PA) {INT i ; float av, s = 0; for (i = 0; i <5; i ) s = S * PA ; AV = S / 5; Return AV;}

Pointer variable pointing to multi-dimensional array

This section describes the pointer variables of the multi-dimensional array in two-dimensional array.

First, the representation of the multi-dimensional array address is equipped with a total two-dimensional array A [3] [4] as follows: 0 1 2 34 5 6 78 9 10 11 Setting the first address of the array A is 1000, the first address of each subscript variable And its value is shown in the figure. In Chapter 4, the C language allows a two-dimensional array to decompose into multiple one-dimensional arrays. Therefore, array A can be broken down into three one-dimensional arrays, namely A [0], A [1], A [2]. Each one-dimensional array has four elements. For example, a [0] array contains A [0] [0], A [0] [1], A [0] [2], A [0] [3] four elements. The address of the array and array elements is shown below: A is a two-dimensional number of group names and the first address of the 2D array 0 row, equal to 1000. A [0] is the number of group names and first addresses of the first one-dimensional array, so it is also 1000. * (A 0) or * a is equivalent to A [0], which represents the first-dimensional array a [0] No. 0 element. Also 1000. & a [0] [0] is the 0 row of 0 rows of two-dimensional array A, which is also 1000. Therefore, A, A [0], * (A 0), * a, & a [0] [0] are equal. Similarly, A 1 is the first address of the two-dimensional array 1 line, equal to 1008. A [1] is the number of group names and first addresses of the second one-dimensional array, so it is 1008. & a [1] [0] is a 1 line 0 column element address of the two-dimensional array A, is also 1008. Therefore, A 1, A [1], * (A 1), & a [1] [0] is equivalent. This can be obtained: A I, A [I], * (A I), & a [i] [0] is equivalent. In addition, & a [i] and a [i] are also equivalent. Because the & a [i] cannot be understood as the address of Element A [I] in the two-dimensional array, there is no element a [i]. C language regulations, it is an address calculation method that represents an array A X-line address. As a result, we conclude: a [i], & a [i], * (a i) and a i are also equivalent. In addition, A [0] can also be regarded as a first address of the No. 0 element of one-dimensional array a [0], and a [0] 1 is an element of A [0] The first address, thereby obtaining a [i] j is a one-dimensional number of group J, the first address, which is equal to & a [i] [j]. A [i] = * (A i) is a [i] j = * (A I) J, due to * (A I) J is the i line J column elements of the two-dimensional array A First address. The value of this element is equal to * (* (A I) J). [EXPLAIN] #define pf "% D,% D,% D,% D,% D, / N" main () {static int a [3] [4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; Printf (PF, A, * A, A [0], & A [0], & A [0] [0]); Printf (PF, A 1 , * (a 1), a [1], & a [1], & a [1] [0]); PrintF (PF, A 2, * (A 2), A [2], & a [2 ], & a [2] [0]); PrintF ("% D,% D / N", A [1] 1, * (A 1) 1); Printf ("% D,% D / N ", * (A [1] 1), * (* (A 1) 1));

Second, the pointer variable of multi-dimensional array

Decompose the two-dimensional array A into a one-dimensional array a [0], A [1], A [2], set P to point to the pointer variable to the two-dimensional array. Can be defined as: int (* p) [4] It indicates that P is a pointer variable, which points to the two-dimensional array a or pointing to the first one-dimensional array a [0], its value is equal to A, A [0], or & a [0] [0], etc. And P I points to the one-dimensional array a [I]. From the previous analysis, * (p i) j is the address of the element of the two-dimensional array I line J column, and * (* (p i) j) is the value of the i line J column element.

The general form of the two-dimensional array pointer variable description is: Type Design (* Pointer Variable Name) [Length] where "Type Descriptor" is the data type of the index group. "*" Indicates that the variables thereafter are pointer types. "Length" means that the two-dimensional array is decomposed into a plurality of one-dimensional array, the length of the one-dimensional array is the number of columns of the two-dimensional array. It should be noted that "(* pointer variable name)" is not less brackets on both sides. If the lack of parentheses, it is said that it is a pointer (introduced later in this chapter), and the meaning is completely different. [Explain] main () {static int A [3] [4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; int (* p) [4 ]; INT I, J; P = a; for (i = 0; i <3; i ) for (j = 0; j <4; j ) Printf ("% 2d", * (* (p i) J));} 'Description of the' Expain string pointer variable and definition of string pointer variable Description is the same as the pointer variable indicating the character variable. It can only be different from the assignment of the pointer variable. The pointer variable to the character variable should give the address of the character variable. Such as: CHAR C, * P = & C; indicating that P is a pointer variable to character variable C. And: char * s = "c language"; indicating that S is a pointer variable pointing to the string. Give the first address of the string to s. Please see an example below. Main () {char * ps; ps = "c language"; Printf ("% s", ps);} Run results are: C language, first define PS is a character pointer variable, then put strings The first address gives PS (which should write the entire string so that the compilation system sets the string into a continuous memory unit) and send the first address to the PS. Program: char * ps; ps = "c language"; equivalent to: char * ps = "c language"; all characters after the n characters in the string are output. Main () {char * ps = "this is a book"; int N = 10; ps = ps n; Printf ("% s / n", ps);} Run results are: Book Input PS initialization That is, the character serial address is given to the PS. After the PS = PS 10, the PS points to the character "B", so the output is "BOOK". Main () {char ST [20], * ps; INT I; Printf ("INPUT A String: / N"); PS = St; Scanf ("% S", PS); for (i = 0; PS [ I]! = '/ 0'; i ) IF (PS [i] == 'k') {printf ("" there is a 'k' in the string / n "); Break;} IF (PS [i] == '/ 0') Printf ("TheRe IS NO 'K' IN THE STRING / N");} This example is to find a 'K' character in the input string. The following example is to point the pointer variable to a format string, in the Printf function, used to output the value of various address representation of the two-dimensional array. However, the format string is replaced with a pointer variable PF in the PrintF statement. This is also a method commonly used in the program.

Main () {static int A [3] [4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; char * pf; pf = "% D, % D,% D,% D,% D / N "; Printf (PF, A, * A, A [0], & A [0], & a [0] [0]); Printf (PF, A 1 , * (a 1), a [1], & a [1], & a [1] [0]); PrintF (PF, A 2, * (A 2), A [2], & a [2 ], & a [2] [0]); PrintF ("% D,% D / N", A [1] 1, * (A 1) 1); Printf ("% D,% D / N ", * (a [1] 1), * (* (A 1) 1));} In the following example, the string pointer is used as the function parameters. Requires the contents of a string to another string and cannot use the strcpy function. The shape of the function CPRSTR is two character pointer variables. PSS points to the source string, PDS points to the target string. Expression: (* PDS = * PSS)! = `/ 0'cpystr (Char * PSS, Char * PDS) {while (* PDS = * PSS)! = '/ 0') {PDS ; PSS ;}} Main () {char * pa = "China", b [10], * Pb; Pb = B; CPYSTR (PA, PB); Printf ("String A =% S / NSTRING B =% S / N", PA In the above example, the program completed two work: First, copy the source characters pointed to by the PSS to the target character pointing to the PDS, and the second is to determine whether the replicated character is `/ 0 ', if Then indicate that the source string ends and no longer cycling. Otherwise, PDS and PSS are added to 1, pointing to the next character. In the main function, the CPRSTR function is called after the spectrum variable PA, PB is obtained, and the CPRSTR function is called after the determination value is obtained. These strings can be used in the main function and the CPRSTR function because the pointer variables PA and PSS, PB, and PDS are used to point to the same string. You can also simplify the CPRSTR function to the following form: CPRSTR (Char * PSS, CHAR * PDS) {while (* PDS = * PSS )! = `/ 0 ');} That is, the movement and assignment of the pointer in one statement in. Further analysis can also be found that the US / 0 ''s ASCII code is 0. For the While statement, only the value of the expression is not 0, and 0 ends cycle, so it can also save "! =` / 0' " One judgment section, is written as the following form: CPRSTR (CHAR * PSS, CHAR * PDS) {while (* PDSS = * PSS );} The meaning of the expression can be interpreted as that the source character assigns the target character, the mobile pointer, if The value is not 0 cycle, otherwise the cycle is ended. This makes the program more concise. The simplified procedure is as follows. CPYSTR (CHAR * PSS, CHAR * PDS) {while (* PDS = * PSS );} main () {char * PA = "China", B [10], * Pb; PB = B; CPYSTR (Pa, PB ); Printf ("string a =% s / nstring b =% S / N", PA, PB);} The difference between the character string pointer variable and the character array

The storage and operation of the string can be implemented with the character array and character pointer variable. But both are different. Note the following questions when using: 1. The string pointer variable itself is a variable for the first address of the string. The string itself is stored in a continuous memory space headed by the first address and is ended as a string as a string. The character array is due to several array elements, which can be used to store the entire string.

2. Assignment assignment to the character number group, must be used in external types or static types, such as: static char st [] = {"c language"}; no such limit for string pointer variables, such as: char * ps = " ""

3. The string pointer method char * ps = "c language"; can be written as: char * ps; ps = "c language"; and alternatively: static char st [] = {"c language"}; Written as: CHAR ST [20]; ST = {"c language"}; "only assigns each element of the character array.

From the above point, you can see the difference between the string pointer variable and the character array in use, and it can also be more convenient to use the pointer variable. As mentioned earlier, when a pointer variable is dangerous before the determination is not obtained, it is easy to cause errors. However, the direct assignment of the pointer variable is possible. Because the C system is assigned to the pointer variable to the determined address. Therefore, char * ps = "c langage"; or char * ps; ps = "c language"; all legal.

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

New Post(0)