Pointer variable of multi-dimensional array

Source: Internet
Author: User

I. Multi-dimensional array address Representation

There is an integer two-dimensional array a [3] [4] as follows:

0 1 2 3
4 5 6 7
8 9 10 11

Set the first address of array a to 1000, and the first address and value of each subscript variable.

As mentioned earlier, the C language allows a two-dimensional array to be decomposed into multiple one-dimensional arrays for processing. Therefore, array A can be divided into three one-dimensional arrays, namely, a [0], a [1], and a [2]. Each one-dimensional array contains four elements. For example, the array a [0] contains a [0] [0], a [0] [1], a [0] [2], A [0] [3] four elements. The addresses of arrays and array elements are as follows: A is the name of a two-dimensional array and the first address of zero rows of a two-dimensional array, which is equal to 1000. A [0] is the array name and first address of the first one-dimensional array, so it is also 1000. * (A + 0) or * A is equivalent to a [0]. It represents the first address of element a [0] 0 in a one-dimensional array. It is also 1000. & A [0] [0] is the first address of the element in column 0 of row 0 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 line 1 of the Two-dimensional array, which is equal to 1008. A [1] is the array name and first address of the second one-dimensional array, so it is also 1008. & A [1] [0] is the address of the element column 0 in the first row of two-dimensional array A, which is also 1008. Therefore, a + 1, a [1], * (a + 1), and & A [1] [0] are equivalent. From this we can conclude that a + I, a [I], * (a + I), and & A [I] [0] are equivalent. In addition, & A [I] is equivalent to a [I. In a two-dimensional array, & A [I] cannot be understood as the address of element a [I], and element a [I] does not exist.

The C language specifies that it is an address calculation method, indicating the first address of line I of array. From this, we can conclude that a [I], & A [I], * (a + I) and a + I are also equivalent. In addition, a [0] can also be considered as a [0] + 0, which is the first address of element 0 of the one-dimensional array a [0, A [0] + 1 is the first address of element 1 of a [0, from this we can conclude that a [I] + J is the first address of element J of one-dimensional array a [I], which is equal to & A [I] [J]. Obtained from a [I] = * (a + I) A [I] + J = * (a + I) + J, because * (a + I) + J is the first address of the element in column J of the I row of the Two-dimensional array. The value of this element is equal to * (a + I) + J ).

[Explain]

# Define pf "% 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 ));
}

2. pointer variables of multi-dimensional arrays

After dividing two-dimensional array a into one-dimensional arrays A [0], a [1], a [2], Set P to a pointer variable pointing to a two-dimensional array. It can be defined as: int (* P) [4], which indicates that p is a pointer variable, which points to two-dimensional array a or to the first one-dimensional array a [0], the value is equal to a, a [0], or & A [0] [0. P + I points to the one-dimensional array a [I]. From the preceding analysis, we can conclude that * (p + I) + J is the address of the element in column J of the Two-dimensional array I, and * (p + I) + J) it is the value of column J of row I.

The two-dimensional array pointer variables are generally described as follows: type specifiers (* pointer variable name) [length] Where "type specifiers" are the data types of the specified array. "*" Indicates that the variable after it is a pointer type. "Length" indicates the length of the one-dimensional array, that is, the number of columns of the Two-dimensional array, when the two-dimensional array is divided into multiple one-dimensional arrays. Note that brackets on both sides of "(* pointer variable name)" are indispensable. If brackets are missing, they indicate pointer arrays (which will be described later in this chapter). 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 =;
For (I = 0; I <3; I ++)
For (j = 0; j <4; j ++) printf ("% 2D", * (p + I) + j ));
}

The description of 'expain string pointer variable and the definition of using string pointer variable are the same as that of pointing to character variable. You can only assign different values to pointer variables. Assign the address of the character variable to the pointer variable pointing to the character variable. For example, char C, * P = & C; indicates that p is a pointer variable pointing to the character variable C. Char * s = "C Language"; indicates that S is a pointer variable pointing to a string. Assign the first address of the string to S.

See the following example.

Main (){
Char * pS;
PS = "C Language ";
Printf ("% s", PS );
}

The running result is:

C Language

In the above example, we first define PS as a character pointer variable, and then assign the first address of the string to PS (the entire string should be written so that the compilation system can load the string into a continuous memory unit ), and send the first address to PS. In the program: char * pS; PS = "C Language"; equivalent to: char * PS = "C Language"; all characters After n characters in the output string.

Main (){
Char * PS = "this is a book ";
Int n = 10;
PS = Ps + N;
Printf ("% s/n", PS );
}

The running result is:

When the book initializes the PS in the program, the first address of the string is given to PS. When PS = Ps + 10, 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 ");
}

In this example, check whether the 'K' character exists in the input string. The following example points the pointer variable to a format string, which is used in the printf function to output values represented by various Addresses of two-dimensional arrays. However, in the printf statement, the pointer variable PF is used to replace the format string. This is also a common method 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/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 ));
}

The following example describes how to use a string pointer as a function parameter. The content of a string must be copied to another string, and the strcpy function cannot be used. The form parameter of the function cprstr is a two-character pointer variable. Pss refers to the source string, and Ps' points to the target string. Expression:

(* PPS = * PSS )! = '/0'
Cpystr (char * PSS, char * PPS ){
While (* PPS = * PSS )! = '/0 '){
PPS ++;
Pss ++ ;}
}
Main (){
Char * pA = "China", B [10], * pb;
PB = B;
Cpystr (Pa, Pb );
Printf ("string a = % S/nstring B = % s/n", Pa, Pb );
}

In the above example, the program has completed two tasks: one is to copy the source character pointed to by PSS to the target character pointed to by, the second is to determine whether the copied character is '/0'. If so, it indicates that the source string ends and does not loop. Otherwise, you must add 1 to both the PDDs and PSS to point to the next character. In the main function, take the pointer variable Pa and Pb as the real parameters and obtain the determined values respectively, then call the cprstr function. Because the pointer variables Pa and PSS, Pb and PDS both point to the same string, these strings can be used in the main function and cprstr function. You can also simplify the cprstr function to the following form:

Cprstr (char * PSS, char * PPS)
{While (* PPS ++ = * PSS ++ )! = '/0 ');}

That is, the pointer movement and value assignment are combined in a statement. Further analysis also shows that the ASC ⅱ code of '/0' is 0. For a while statement, if the value of the expression is not 0, the loop ends, therefore, you can save "! = '/0', which is written in the following form:

Cprstr (char * PSS, char * PPS)
{While (* PDSS ++ = * PSS ++ );}

The meaning of the expression can be interpreted as: the source character is assigned a value to the target character and the pointer is moved. If the value is not 0, the loop ends. This makes the program more concise. The simplified program is as follows.

Cpystr (char * PSS, char * PPS ){
While (* PPS ++ = * PSS ++ );
}
Main (){
Char * pA = "China", B [10], * pb;
PB = B;
Cpystr (Pa, Pb );
Printf ("string a = % S/nstring B = % s/n", Pa, Pb );
}

Differences between string pointer variables and character Arrays

Both character arrays and character pointer variables can be used to store and operate strings. However, the two are different. Pay attention to the following issues during use:

1. The string pointer variable is a variable used to store the first address of a string. The string itself is stored in a contiguous memory space headed by this first address and ended with '/0. Character array is composed of several array elements, which can be used to store the entire string.

2. initialize and assign values to character arrays, which must be of an external or static type, for example, static char st [] = {"C Language"}. This restriction is not imposed on string pointer variables, for example, char * PS = "C Language ";

3. For the string pointer mode char * PS = "C Language"; can be written as: char * pS; PS = "C Language"; for the array mode:

Static char st [] = {"C Language "};

It cannot be written as follows:

Char st [20]; ST = {"C Language "};

You can only assign values to each element of the character array one by one.

From the above points, we can see the difference between the string pointer variable and the character array in use. It can also be seen that it is more convenient to use the pointer variable. As mentioned above, it is dangerous to use a pointer variable before obtaining a definite address, which may cause errors. However, you can assign values to pointer variables directly. This is because the C system must give a definite address when assigning values to pointer variables. Therefore,

Char * PS = "C langage ";

Or

Char * pS;
PS = "C Language ";

Are valid.

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.