This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13036|Reply: 0

[C] The input of the two-digit character array is sorted when the output is made

[Copy link]
Posted on 12/14/2015 3:27:26 PM | | |

#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) */






Previous:C# specifies the IP segment to generate the IP address
Next:Cross-thread operation not valid: Control 'txtresult' accessed from a threa...
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com