Date of writing: 2016.08.31
AC qq:992591601
Spent a few days reviewing the C language. For the C language of the string operation some are not used to, so as an exercise, wrote down the following such a wrong program:
#include <stdio.h>#defineMax_str_size 100voidCopy_string (Char* from,Char*To ) { while(*to++ = * from++);}voidSwap_strs (Char* STR1,Char*str2) { CharTmp[max_str_size]; Copy_string (STR1, TMP); Copy_string (str2, str1); Copy_string (TMP, STR2);}voidSort_strs_by_ascii (Char* arr[3]) { if(strcmp (arr[0], arr[1]) <0) swap_strs (arr[0], arr[1]); if(strcmp (arr[1], arr[2]) <0) swap_strs (arr[1], arr[2]); if(strcmp (arr[0], arr[1]) <0) swap_strs (arr[0], arr[1]);}intMain () {Char* arr[3] = {"DD","AA","cc"}; Sort_strs_by_ascii (arr); for(inti =0; I <3; i++) printf ("%s\n", Arr[i]); return 0;}
This program is certainly not very good, mainly to practice C, deliberately to use something, such as a C-string pointer, pointer array.
The array in the program holds three string pointers. Use the array as a parameter to sort by the Sort_strs_by_ascii method. But the error is in the process of string manipulation.
Then I learned that char* arr is a string pointer that points to a value that is stored in a constant area and cannot be rewritten. However, the pointer can be arbitrarily pointed to other address spaces.
While an array pointer such as Char arr[] is essentially a pointer, the pointer to the content is fixed in the memory space of the array. However, the contents of the array space can be rewritten.
The difference between char* arr and char[] Arr