#include "stdafx.h" #include "stdio.h" void out_student(char (*p)[20],int n); char (*p)[20]Data pointer (row pointer) void sort_student(char (*p)[20],int n); int main() {char students[3][20]; int i; for(i=0; i<3; i++) scanf("%s",*(students+i)); Entered out_student(students,3); sort_student(students,3); out_student(students,3); return 0;
} void out_student(char (*p)[20],int n) //(*p) line address {int i; for(i=0; i<n; i++) printf("%s",*(p+i)); printf("\n");
}
//选择排序 void sort_student(char (*p)[20],int n) {int i,j; for(i=0; i<n-1; i++) {int pos=i; for(j=i+1; j<n; j++) {if(strcmp(*(p+j),*(p+pos)<0)) //strcmp(*(p+j),*(p+pos)<0) *(p+j) cannot be a row address, it must be a column address, the first address of a first-order array {pos=j; }
}
} if(pos!=i) {char str[20]={'\0'}; strcpy(str,*(p+pos)); *(p+pos) The address of the first column of the pos line, the swap string strcpy(*(p+pos),*(p+i)); strcpy(*(p+i),str);
}
}
//总结:
/*二维数组表示(行地址,列地址) The pointer traverses the array 2D character array (row address, column address) */
|