C Language Learning tutorial sixth chapter-pointers (6)

Source: Internet
Author: User
Tags array length arrays comparison integer printf sort strcmp

The general form of the pointer array description is: Type descriptor * array name [array length]
Where the type descriptor is the type of the variable to which the pointer value points. For example: int *pa[3] means that the PA is an array of pointers, it has three array elements, and each element value is a pointer to an integer variable. You typically use an array of pointers to point to a two-dimensional array. Each element in the pointer array is given the first address of each row of the two-dimensional array, so it can also be understood to point to a one-dimensional array. Figure 6-6 shows this relationship.
int a[3][3]={1,2,3,4,5,6,7,8,9};
int *pa[3]={a[0],a[1],a[2]};
int *p=a[0];
Main () {
int i;
for (i=0;i<3;i++)
printf ("%d,%d,%d\n", a[i][2-i],*a[i],* (* (a+i) +i));
for (i=0;i<3;i++)
printf ("%d,%d,%d\n", *pa[i],p[i],* (P+i));
}
In this example, the PA is an array of pointers, and three elements point to each row of the two-dimensional array A. The specified array element is then output with a circular statement. Where *a[i] represents the i row 0 column element value; * (* (a+i) +i) represents the element value of the I row I column, *pa[i] represents the i row 0 column element value, and because P is the same as A[0, P[i] represents the value of the 0 row i column, and * (P+i) represents the value of the 0 row I column. Readers can carefully understand the various representations of element values. You should pay attention to the difference between the pointer array and the two-dimensional array pointer variable. Both of these can be used to represent two-dimensional arrays, but their presentation methods and meanings are different.

A two-dimensional array pointer variable is a single variable, and the parentheses on both sides of the general form "(* pointer variable name)" are not small. The pointer array type represents more than one pointer (a set of ordered pointers) that cannot have parentheses on either side of the "* Pointer array name" in general form. For example: Int (*p) [3]; Represents a pointer variable that points to a two-dimensional array. The number of columns in the two-dimensional array is 3 or decomposed to a one-dimensional array with a length of 3. int *p[3] means that p is an array of pointers, with three subscript variables p[0],p[1],p[2] being pointer variables.

An array of pointers is also used to represent a set of strings, at which point each element of the pointer array is given the first address of a string. The initialization of an array of pointers to a string is simpler. For example, in example 6.20, an array of pointers is used to represent a set of strings. Its initialization assignment is:
Char *name[]={"Illagal Day",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"};
After completing this initialization assignment, name[0] points to the string "Illegal Day", name[1] refers to the quot; Monday ".....

An array of pointers can also be used as function arguments. In this example main function, a pointer array name is defined and the name is initialized for assignment. Each of its elements points to a string. The pointer-type function day name is then called with name as the argument, and the array name name is given the shape parametric name at call time, and the integer I input is given the parameter n as the second argument. A value of two pointer variables pp1 and PP2,PP1 assigned to NAME[0] is defined in the day name function (that is, *name), PP2 is assigned the value of Name[n] (name+ N). The conditional expression decides to return the PP1 or pp2 pointer to the pointer variable PS in the main function. The last output is the value of I and PS.

An array of pointers as a parameter to a pointer type function
Main () {
static char *name[]={"Illegal Day",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"};
Char *ps;
int i;
Char *day_name (char *name[],int N);
printf ("Input Day no:\n");
scanf ("%d", &i);
if (i<0) exit (1);
Ps=day_name (Name,i);
printf ("Day no:%2d-->%s\n", i,ps);
}
Char *day_name (char *name[],int N)
{
Char *pp1,*pp2;
Pp1=*name;
pp2=* (Name+n);
Return (n<1| | N&GT;7)? PP1:PP2);
}
The following example requires 5 names to be entered and then output in alphabetical order. In the previous example, a normal sorting method was used to compare the position of the string after each comparison. The physical location of the swap string is done through a string copy function. Repeated swapping will slow the execution of the program and increase the burden of storage management because of the different lengths of each string (country name). Using an array of pointers is a good way to solve these problems. Put all the strings in an array, place the first address of these character arrays in an array of pointers, and when you need to swap two strings, simply swap the contents (addresses) of the corresponding two elements of the pointer array without swapping the string itself. Two functions are defined in the program, one named sort completes the sort, and its formal parameter refers to the
The PIN array name, which is a pointer to the array of strings to be sorted. The number of formal parameter n is a string. The other function, named Print, is used to sort the output of the string, and its shape participates in the same parameters as the sort. In main function main, you define the pointer array name and initialize the assignment. The sort function and the print function are then called to complete the sorting and output respectively. It is worth noting that in the sort function, the strcmp function is used to compare two strings, and the strcmp function allows the strings participating in the comparison to appear as pointers. Name[k] and name[J] are pointers and are therefore legitimate. When you need to swap after a string comparison, swapping only the values of the pointer array elements without swapping the specific strings will greatly reduce the overhead of the time and increase the efficiency of your operation.
It is programmed as follows:
#include "string.h"
Main () {
void sort (char *name[],int n);
void print (char *name[],int n);
Static char *name[]={"in", "AMERICA", "AUSTRALIA",
"FRANCE", "GERMAN"};
int n=5;
Sort (name,n);
Print (name,n);
}
void sort (char *name[],int N) {
Char *pt;
int i,j,k;
for (i=0;i<n-1;i++) {
K=i;
for (j=i+1;j<n;j++)
if (strcmp (Name[k],name[j]) >0) k=j;
if (k!=i) {
Pt=name[i];
NAME[I]=NAME[K];
name[k]=pt;
}
}
}
void print (char *name[],int n) {
int i;
for (i=0;i<n;i++) printf ("%s\n", Name[i]);
}

Related Article

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.