Number of strings in alphabetical order from large to small output
Pointer array, array of pointers format: int *p[]
The code is as follows:
#include <iostream> #include <string>using namespace Std;int main () { void sort (char *name[],int n); function declaration void print (char *name[],int n); Char *name[]={"BASIC", "FORTRAN", "C + +", "Pascal", "COBOL"};//define pointer array int n=5; Sort (name,n); function call print (name,n); return 0;} The void sort (char *name[],int n) //sort function is to sort the string (select sort), the parameter name is the pointer array name, and the first element of the real parameter group name is accepted as the address { char *temp; int i,j,k; for (i=0;i<n-1;i++) { k=i; K indicates the minimum positional record for the keyword for (j=i+1;j<n;j++) if (strcmp (Name[k],name[j]) >0) k=j; if (k!=j) { temp=name[i];name[i]=name[k];name[k]=temp;//name[k] and Name[j] are the starting addresses of the K and J Strings } } }void print (char *name[],int N) //name[0] and name[4] are the first addresses of each string (sorted from large to small order) { int i; for (i=0;i<n;i++) Cout<<name[i]<<endl;}
Operation Result:
Note: The pointer array name is defined in the main function. It has 5 elements, whose initial value is the starting address of a string, respectively.
The print function can also be written in the following form:
void print (char *name[], int n)
{
int i=0;
Char *p;
P=NAME[0];
while (I<n)
{
p=* (name+i++);
cout<<p<<endl;
}
}
where "* (name+i++)" means the address of the first (name+i) value, or name[i]. Assign it to P, then I plus 1. Finally, a string that starts with the P address is output.
Copyright NOTICE: This article is my original article, we can reprint Oh!!
Array of pointers