Recently I came across a more comprehensive topic

Well, to describe the problem, first enter a number N, which means that there will be N strings of input

Note: N cannot exceed 10 and each string cannot exceed 20 in length

We’re going to write a program that prints the N strings in ascending order.

#include <string.h> int main() { char ch[10][21]; Char [10][50]; char[10][50]; char[10][50]; */ int I,j,iCnt=0; ICnt while (1) {scanf("%s",ch[iCnt]); iCnt++; If (STRCMP (ch[icnt-1],"#") == 0) {if (STRCMP (ch[icnt-1],"#") == 0) { } } iCnt--; // Bubble sort the two-dimensional character array by each line for(I =0; i<iCnt-1; i++){ for(j=i+1; j<iCnt; j++) if ( strlen(ch[i]) > strlen(ch[j]) ) { char middle[60]; strcpy(middle,ch[i]); strcpy(ch[i] ,ch[j]); strcpy(ch[j] ,middle); /*strcpy char s[50]="ascasc"; char z[50]="lll"; Such as strcpy (s, z); */}} // outputs sorted for(I =0; i<iCnt; i++) printf("%s " ,ch[i]); return 0; }Copy the code

That’s the complete code, with a few interesting and noteworthy details to point out:

First of all, when we were defining a two-dimensional array, inside the second square bracket we said [21], because we had to take into account the final terminator, in case we did enter 20.

The second point is that arrays are essentially Pointers

scanf("%s" ,ch[iCnt]);
Copy the code

We apply this to the input string, where ch [0] represents the address of the first string.

Then there is my understanding of strcpy:

Strcpy char s[50]="ascasc"; char z[50]="lll"; Such as strcpy (s, z); At this point s becomes "LLL" even though there's something else */Copy the code

This is different from my first impression. In the example above, my first instinct was to get llLASC.

With that in mind, I looked up and learned the prototype of strcpy

char *strcpy(char *strDest, const char *strScr) { char *address=strDest; assert((strDest ! = NULL) && (strScr ! = NULL)); While (*strScr) // is while(*strScr! = '/0'); { *strDest++ = *strScr++; } *strDest = '/0'; // If strScr is shorter than strDest, return address; //, if the statement is not changed, it will be an error. } ———————————————— Copyright notice: This article is originally published BY CSDN blogger "Nadakiss" under CC 4.0 BY-SA copyright agreement. Please attach the link to the original source and this statement. The original link: https://blog.csdn.net/nadakiss/article/details/6259603Copy the code

*strDest=’\0′ =’ strDest=’\0′ =’ strDest=’\0′

But here, assert() is a function you’ve never seen before, so go back and learn about it:

The assert macro is prototypically defined in <assert.h> to terminate program execution if its condition returns an error. #include <assert.h> void assert(int expression); Assert evaluates expression first. If its value is false (that is, 0), it prints an error message to stderr and then terminates the program by calling abort.Copy the code

This function can be used to check the validity of the transmitted content to ensure the robustness of the entire code.