I asked two second-level C language pointer questions yesterday and re-examined myself. The C language is really a dish, and I have no idea what it is !!!! Send this article to check for missing information.
The following code is used to sort strings.
#include <stdio.h> #include <string.h>void main(){ FILE *p; char *s="634,.%@\\w|sq2",c; int i,j,k,n=strlen(s); p=fopen("Design2.dat","w"); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if( *(s+j)<*(s+i) ) {c=*(s+i); *(s+i)=*(s+j); *(s+j)=c; } for(i=0;i<n;i++) fputc(s[i],p); fclose(p); }
II:
#include <stdio.h>#include <string.h>void main(){ //FILE *p; int i,j,k,n=strlen(s); // p=fopen("Design2.dat","w"); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if( *(s+j)<*(s+i) ) {c=*(s+i); *(s+i)=*(s+j); *(s+j)=c; } for(i=0;i<n;i++) printf("%c",s[i]); //fputc(s[i],p); //fclose(p); }
The above section of the program can not run, changed a lot, and finally simply changed all the pointers to an array, you can use it. And changed to only the definition when the array is left, the other part is changed back to the pointer.
Then I asked mango (ym, omnipotent) and found a problem that I ignored:
Char * s = {"634,. % @ \ w | sq2 "}
This definition is incorrect, because no space is allocated.
ChangeChar s [] = {"634,. % @ \ w | sq2"} OK
Another question has also been raised, and I have been asking for advice in the forum (the first time I posted the forum... ^_^ ).
# Include <stdio. h> main () {int I, j; char CH, * P1, * P2, * s [4] = {"four", "hello", "peak ", "Apple"}; for (I = 0; I <4; I ++) {p1 = P2 = s [I]; // use P1 and P2 as address exchanges ?? Ch = * (P1 + I); While (* P1! = '\ 0') {If (* P1! = CH) {* P2 = * P1; P2 ++;} P1 ++;} * P2 = '\ 0';} for (I = 0; I <4; I ++) printf ("% s \ n", s [I]);}
*s[4]={"four","hello","peak","apple"};
The problem is that, in the same-care mode, no space is allocated .... Bytes