C Language Learning tutorial sixth chapter-pointers (4)

Source: Internet
Author: User
Tags printf

Second, the pointer variable of multidimensional array

After the two-dimensional array A is decomposed into one-dimensional array a[0],a[1],a[2], p is set to a pointer variable that points to a two-dimensional array. can be defined as: Int (*p) [4] It means that p is a pointer variable that points to a two-dimensional array A or to the first one-dimensional array a[0], whose value equals a,a[0], or &a[0][0]. P+i, however, points to a one-dimensional array a[i]. From the previous analysis, it can be concluded that the * (P+i) +J is the address of the element in the two-dimensional array I row j, and that * (* (p+i) +j) is the value of the I-row J-Column element.

The general form of descriptions of two-dimensional array pointer variables is: type specifier (* pointer variable name) [length] where "type specifier" is the data type of the indexed group. "*" indicates that the subsequent variable is a pointer type. Length represents the length of a one-dimensional array, or the number of columns in a two-dimensional array, when a two-dimensional array is decomposed into multiple one-dimensional arrays. Note that the brackets on both sides of the (* pointer variable name) are not small, such as missing parentheses means an array of pointers (described 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));
}
The description of the ' Expain string pointer variable and the definition of using a string pointer variable are the same as the description of the pointer variable pointing to a character variable. can only be distinguished by the assignment of the pointer variable to a different value. The pointer variable that points to a character variable should be given the address of the character variable. For example, Char c,*p=&c; indicates that p is a pointer variable that points to the character variable C. and: Char *s= "C Language"; s is a pointer variable that points to a string. Give the first address of the string to S.
Take a look at the example below.
Main () {
Char *ps;
ps= "C Language";
printf ("%s", PS);
}
The results of the operation are:
C Language
In the example above, you 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 system can load the string into a continuous piece of internal deposit) and send the first address to PS. Program: Char *ps;ps= "C Language"; is 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 results of the operation are:
When the book initializes the PS in the program, it assigns the first address of the string to PS, and 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]!= ' n '; i++)
if (ps[i]== ' K ') {
printf ("There is a ' K ' string\n");
Break
}
if (ps[i]== ' n ') printf ("There is no ' K ' string\n");
}
This example looks for a ' k ' character in the input string. The following example refers to the pointer variable to a format string, used in a printf function, to output the values of the various address representations of a two-dimensional array. However, the pointer variable pf is substituted for the format string in the printf statement. This is also the 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 use of a string pointer as a function parameter is explained. Requires that the contents of one string be copied into another string, and the strcpy function cannot be used. A function cprstr a two-character pointer variable. PSS points to the source string, and the PDS points to the destination string. An expression:
(*PDS=*PSS)!= ' "
Cpystr (char *pss,char *pds) {
while ((*PDS=*PSS)!= ' ") {
pds++;
pss++; }
}
Main () {
Char *pa= "B[10],*PB";
Pb=b;
Cpystr (PA,PB);
printf ("String a=%s\nstring b=%s\n", PA,PB);
}
In the example above, the program completes two tasks: the first is to copy the source characters that the PSS points to the target characters pointed to by the PDS, and the second is to determine whether the copied characters are ' "", and if it indicates that the source string ends and no longer loops. Otherwise, the PDS and PSS all add 1, pointing to the next character. In the main function, take the pointer variable pa,pb as the argument, and then call the CPRSTR function after obtaining the definite value respectively. Because the pointer variable PA and PSS,PB and PDS all point to the same string, these strings are available in both the main function and the CPRSTR function. You can also simplify the CPRSTR function to the following form:
Cprstr (char *pss,char*pds)
{while ((*pds++=*pss++)!= ' ");}
That is, the movement and assignment of the pointer is merged into a single statement. Further analysis can also be found that the ' Ascⅱ ' code is 0, while the while statement only look at the expression value is not 0 loops, 0 to end the loop, so you can also omit the "!= '" "This part of the judgment, but written in the following form:
Cprstr (char *pss,char *pds)
{while (*pdss++=*pss++);}
The meaning of an expression can be interpreted as the source character assignments value to the target character, moving the pointer, or ending the loop if the assignment is not 0. This makes the program more concise. The simplified procedure is shown below.
Cpystr (char *pss,char *pds) {
while (*pds++=*pss++);
}
Main () {
Char *pa= "B[10],*PB";
Pb=b;
Cpystr (PA,PB);
printf ("String a=%s\nstring b=%s\n", PA,PB);
}

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.