Some issues I ignored (string pointer)

Source: Internet
Author: User

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.