[Zt] array pointer

xiaoxiao2021-03-06  43

Pointer array 1, the meaning of the pointer array and the definition pointer an array, that is, each element in the array is a pointer. Example 4.5.17 Look at the following program #include #include void main () {char * proname [] = {"fortran", "c", "c "}; cout << Proname [0] << Endl << Proname [1] << endl << proname [2] << endl;} The output is as follows: Fortran C C

Description: From the statement of the above program char * proname [] = {"fortran", "c", "C "}; It can be seen that the type of the array variable proname [] is char *, that is, each of its data The data type of the element is a character pointer. Therefore, the variable PRONAME is called a pointer array.

2, the difference between the pointer array and the two-dimensional array looks to the following variable definition char name1 [3] [8] = {"fortran", "c", "c "}; // This is a two-dimensional character array char * Name2 [] = {"fortran", "c", "c "}; // This is the difference between the argument array pointer array and the two-dimensional array is: the two-dimensional character array only stores character elements, and the character type pointer array divided The character pointer is stored outside the character element. That is, the difference between the two is not the same as the storage form. For example, an array name1 will occupy 3 * 8 = 24 storage space, while Name2 will take up 6 8 2 4 = 20 storage space.

Example 4.5.18 Look at the following program #include #include void main () {char * pro [] = {"fortran", "c", "c "}; char propo1 [ 3] [8] = {"fortran", "c", "c "}; cout << pro [0] << endl << pro [1] << endl << pro [2] << endl; cout << Pro1 [0] << Endl << pro1 [1] << Endl << Pro1 [2] << Endl;} Execution is: Fortran C C Fortran C C is visible, both of which are basically the same. Example 4.5.19 Look at the results of the following procedures #include #include void main () {char ** p; char a = 'x', * q = & a; p = & q; Cout << ** p << endl;} The output result is: X

Here ** is * (* P), since * has a right bonding, parentheses can be omitted. In fact, the two-dimensional array is a pointer to the pointer. A [i] [j] can also be represented by a pointer method: * (* (A I) J) Here * (A i) is a [i], * (* (A I) J) That is, the J position is off is off, that is, A [I] [J]. Example 4.5.20 Look at the results of the following procedures #include #include void main () {char p [3] [6] = {"Hello", "Good", "abcde" }; For (int i = 0; i <= 2; i ) {for (int J = 0; * (* (p i) j)! = '/ 0'; J ) cout << * (* (p i) j); COUT << Endl;}} output is: Hello Good ABCDE3, NULL pointer value NULL is a pointer constant, indicating that the pointer does not point to any address, which can assign any pointer variable. Often used to initialize the pointer. NULL and * VOID Difference: NULL is a pointer constant, which can be assigned to any pointer variable. And * Void is a pointer type and is a null pointer type.

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

New Post(0)