Dark Horse programmer--c Pointers in language (3)

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Pointer variable pointing to a multidimensional array

This section takes a two-dimensional array as an example to introduce pointer variables for multidimensional arrays.

One, multidimensional array address representation method with an integer two-dimensional arrayA[3][4]as follows:0 1 2 3 4 5 6 7 8 9 10 11 Set ArrayaThe first address is +, the first address of each subscript variable and its value. In the fourth chapter, the C language allows a two-dimensional array to be decomposed into multiple one-dimensional arrays for processing. So the arrayacan be decomposed into three one-dimensional arrays, i.e.A[0],A[1],A[2]. Each of the one-dimensional arrays contains four elements. For exampleA[0]array that containsA[0][0],A[0][1],A[0][2],A[0][3]four elements. The address of the array and array elements is represented as follows:ais a two-dimensional array name and a two-dimensional array0the first address of the line, equal to +. A[0]is the array name and first address of the first one-dimensional array, and therefore also the +. * (a+0)or*ais withA[0]equivalent, which represents a one-dimensional arraya[0]0the first address of the number element. Also for +. &a[0][0]is a two-dimensional arrayaof the0Line0the first address of the column element is also +. Therefore,a,A[0],* (a+0),*a?amp;a[0][0]are equal. Similarly,a+1is a two-dimensional array1the first address of the line, equal to1008. A[1]is the array name and the first address of the second one-dimensional array, and therefore also the1008. &a[1][0]is a two-dimensional arrayaof the1Line0the column element address is also1008. Soa+1,a[1],* (a+1), &a[1][0]is the same. The result is:A+i,A[i],* (a+i),&a[i][0]is the same. In addition,&a[i]and theA[i]is the same. Because in a two-dimensional array it is not possible to&a[i]understood as elementsA[i]the address, no element existsA[i].

The C language specifies that it is an address calculation method that represents an arrayaSectionIThe beginning address. Thus, we conclude that:A[i],&a[i],* (a+i)and theA+iare also equal. In addition,A[0]alsocan be seen asa[0]+0is a one-dimensional arrayA[0]of the0the first address of the number element, anda[0]+1is theA[0]of the1the first address of the number element, which can be deriveda[i]+jis a one-dimensional arrayA[i]of theJthe first address of the number element, which equals&a[i][j]. bya[i]=* (a+i)Geta[i]+j=* (a+i) +j, since* (a+i) +jis a two-dimensional arrayaof theILineJthe first address of the column element. 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)); }

Pointer variables for multidimensional arrays

The two-dimensional arrayadecomposed into one-dimensional arraysA[0],a[1],a[2]after that, setPis a pointer variable that points to a two-dimensional array. can be defined as:Int (*p) [4]it representsPis a pointer variable that points to a two-dimensional arrayaor point to the first one-dimensional arrayA[0], whose value is equal toA,a[0], or&a[0][0]and so on. andP+ipoint to a one-dimensional arrayA[i]. From the previous analysis, we can draw* (p+i) +jis a two-dimensional arrayILineJThe address of the element of the column, and* (* (p+i) +j)is theILineJthe value of the column element.

The general form of a two-dimensional array pointer variable description is: type specifier(*pointer variable name)[length] which"Type descriptor"is the data type of the index group. "*"the variable that is followed is the pointer type. "length"Represents the length of a one-dimensional array, that is , the number of columns in a two-dimensional array, when the two-dimensional array is decomposed into multiple one-dimensional arrays. Should note"(*pointer variable name) "the brackets on both sides are not small, such as the absence of parentheses indicates an array of pointers(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=a;

for (i=0;i<3;i++)

for (j=0;j<4;j++)

printf ("%2d", * (* (p+i) +j));

The description of the ' Expain string pointer variable and the definition of the variable using the string pointer are the same as the pointer variable description that points to the character variable. You can only differentiate by assigning different values to pointer variables. A pointer variable that points to a character variable should be given the address of the character variable. such as: char c,*p=&c; represents P is a pointer to a character variable C the pointer variable. And: char *s= "C Language"; it means s is a pointer variable that points to a string. Assigns the first address of the string to s. take a look at the following example.

Main () {

Char *ps;

ps= "C Language";

printf ("%s", PS); The result is: C Language The above example, first defines that PS is a character pointer variable, and then assigns the first address of the string to PS ( the entire string should be written out, In order for the compilation system to load the string into a contiguous block of memory , and send the first address into PS. In the program: char *ps;ps= "C Language"; is equivalent to: Char *ps= "C Language"; in the output string N all characters after a character.

Main () {

Char *ps= "This was a book";

int n=10;

Ps=ps+n;

printf ("%s/n", PS); The result is: Book in the program for PS initialization, that is, the first address of the string 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"); }

This example looks for the ' K ' character in the input string . The following example refers to a pointer variable to a format string, which is used in the printf function to output the values represented by the various addresses of the two-dimensional array. However, the format string is replaced by the pointer variable PF in the printf statement . 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,%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 use of a string pointer as a function parameter is explained. Requires that the contents of one string be copied to another string, and that the strcpy function cannot be used . The parameter of the function Cprstr is a two-character pointer variable. PSS points to the source string, and thePDS 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,PB); }

In the example above, the program has done two tasks: one is toPSSThe source characters pointed to are copied toPDSThe target character that you are pointing to, and the second is whether the copied character is'/0 ', if it indicates that the source string ends and no longer loops. Otherwise,PDSand thePSSall add1, pointing to the next character. In the main function, use the pointer variablePA,PBis an argument and is called when the value is determined separately.Cprstrfunction. Because of the pointer variable usedPAand thePSS,PBand thePDSall point to the same string, so the main function and theCprstrthese strings can be used in functions. You can also putCprstrThe function is simplified to the following form:Cprstr (char *pss,char*pds) {while ((*pds++=*pss++)! = '/0 ');} That is, the movement and assignment of pointers are combined in a single statement. Further analysis can also be found'/0 'of theAscⅱyards for0, for whileThe statement only looks at the value of the expression as non-0on the loop, for0The loop is ended, so you can also save"! = '/0 '"This part of the judgment is written in the following form:Cprstr (char *pss,char *pds) {while (*pdss++=*pss++);} The meaning of the expression can be interpreted as the source character assigned value to the target word, moving the pointer, if the assigned value is a non-0Loop, otherwise end the loop. This makes the program more concise. The simplified program is shown below.

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); }

Dark Horse programmer--c Pointers in language (3)

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.