user-0 提问: 串的填空！求大佬告知代码段的详细作用真没看懂这些代码的目的    要求：采用定长顺序存储表示，由用户创建串s和串t，实现在串s中下标为pos的字符之前插入串t。

回复:
user: user-1
content: 因为有错呗。你这题目没问题？你看看那个main函数里： str2没分配内存（error），i,j,k没使用（warning）。 SString *str1; SString *str2; int pos; int flag=0; str1 = (SString *)malloc(sizeof(SString)); str1->len = 0; str2 = (SString *)malloc(sizeof(SString)); str2->len = 0;

user: user-0
content: 引用 1 楼 霸气侧漏de影子 的回复:#include <stdio.h> #include <stdlib.h> #define MAXLEN 10  typedef struct {          /*串结构定义*/     char ch[MAXLEN];     int len; }SString;  void createstring(SString *s) { int i,j; char c; printf("input the length of the string:"); scanf("%d",&j); for(i=0; i<j; i++) { printf("input the %d:",i+1); fflush(stdin); scanf("%c",&c); ____s->ch[i]=c______  ;} s->len = j; }  void output(SString *s) { int i; for ( i=0;i<s->len;i++) printf("%c   ",s->ch[i]); printf("\n"); }  int StrInsert(SString *s, int pos, SString *t) /*在串s中下标为pos的字符之前插入串t */ { int i; if (pos<0 || pos>s->len) /*插入位置不合法*/ return(0);  if (s->len + t->len<=MAXLEN) /*插入后串长≤MAXLEN*/ { for (i=s->len + t->len-1;i>=t->len + pos;i--) ____s->ch[i]=s->ch[i-t->len]___ ;                 for (i=0;i<t->len;i++)  _____________s->ch[pos+i]=t->ch[i]__; s->len=s->len+t->len; return(1); } else { if (pos+t->len<=MAXLEN) /*插入后串长>MAXLEN,但串t的字符序列可以全部插入*/ { for (i=MAXLEN-1;i>t->len+pos-1;i--)    s->ch[i]=s->ch[i-t->len]; for (i=0;i<t->len;i++)  ___s->ch[pos+i]=t->ch[i]; s->len=MAXLEN; } else /*插入后串长>MAXLEN,并且串t的部分字符也要舍弃*/ {  for (i=0;i<MAXLEN-pos;i++)  __s->ch[pos+i]=t->ch[i]__ ; s->len=MAXLEN; } return(1); } }   void main() { SString *str1; SString  *str2; int i,j,k,pos; int flag=0; str1 = (SString *)malloc(sizeof(SString)); str1->len = 0; printf("creat the string 1:\n"); createstring(str1); printf("creat the string 2:\n"); createstring(str2); printf("input the insert local:"); scanf("%d",&pos); flag=StrInsert(str1,pos,str2); if(flag == 0) printf("insert error!"); else { printf("after insert:\n"); output(str1); }  }   为啥无法执行

user: user-1
content: #include <stdio.h> #include <stdlib.h> #define MAXLEN 10  typedef struct {          /*串结构定义*/     char ch[MAXLEN];     int len; }SString;  void createstring(SString *s) {     int i,j;     char c;     printf("input the length of the string:");     scanf("%d",&j);     for(i=0; i<j; i++)     {         printf("input the %d:",i+1);         fflush(stdin);         scanf("%c",&c);         s->ch[i]=c;     }     s->len = j; }  void output(SString *s) {     int i;     for (i=0;i<s->len;i++)     printf("%c   ",s->ch[i]);     printf("\n"); }  int StrInsert(SString *s, int pos, SString *t) /*在串s中下标为pos的字符之前插入串t */ {     int i;     if (pos<0 || pos>s->len) /*插入位置不合法*/     return(0);     if (s->len + t->len<=MAXLEN) /*插入后串长≤MAXLEN*/     {         for (i=s->len + t->len-1;i>=t->len + pos;i--)             s->ch[i]=s->ch[i-t->len];         for (i=0;i<t->len;i++)             s->ch[pos+i]=t->ch[i];             s->len=s->len+t->len;             return(1);     }     else     {         if (pos+t->len<=MAXLEN) /*插入后串长>MAXLEN,但串t的字符序列可以全部插入*/         {             for (i=MAXLEN-1;i>t->len+pos-1;i--)                 s->ch[i]=s->ch[i-t->len];             for (i=0;i<t->len;i++)                 s->ch[pos+i]=t->ch[i];                 s->len=MAXLEN;         }         else /*插入后串长>MAXLEN,并且串t的部分字符也要舍弃*/         {             for (i=0;i<MAXLEN-pos;i++)                 s->ch[pos+i]=t->ch[i];             s->len=MAXLEN;         }         return(1);     } }   int main() {     SString *str1;     SString *str2;     int pos;     int flag=0;     str1 = (SString *)malloc(sizeof(SString));     str1->len = 0;     str2 = (SString *)malloc(sizeof(SString));     str2->len = 0;     printf("creat the string 1:\n");     createstring(str1);     printf("creat the string 2:\n");     createstring(str2);     printf("input the insert local:");     scanf("%d",&pos);     fflush(stdin);     flag=StrInsert(str1,pos,str2);     if(flag == 0)     {        printf("insert error!");        return(0);     }      else     {         printf("after insert:\n");         output(str1);         return(1);     } }

user: user-0
content: 引用 3 楼 霸气侧漏de影子 的回复:因为有错呗。你这题目没问题？你看看那个main函数里： str2没分配内存（error），i,j,k没使用（warning）。 SString *str1; SString *str2; int pos; int flag=0; str1 = (SString *)malloc(sizeof(SString)); str1->len = 0; str2 = (SString *)malloc(sizeof(SString)); str2->len = 0;那怎么改

user: user-1
content: #include <stdio.h> #include <stdlib.h> #define MAXLEN 10  typedef struct {          /*串结构定义*/     char ch[MAXLEN];     int len; }SString;  void createstring(SString *s) { int i,j; char c; printf("input the length of the string:"); scanf("%d",&j); for(i=0; i<j; i++) { printf("input the %d:",i+1); fflush(stdin); scanf("%c",&c); ____s->ch[i]=c______  ;} s->len = j; }  void output(SString *s) { int i; for ( i=0;i<s->len;i++) printf("%c   ",s->ch[i]); printf("\n"); }  int StrInsert(SString *s, int pos, SString *t) /*在串s中下标为pos的字符之前插入串t */ { int i; if (pos<0 || pos>s->len) /*插入位置不合法*/ return(0);  if (s->len + t->len<=MAXLEN) /*插入后串长≤MAXLEN*/ { for (i=s->len + t->len-1;i>=t->len + pos;i--) ____s->ch[i]=s->ch[i-t->len]___ ;                 for (i=0;i<t->len;i++)  _____________s->ch[pos+i]=t->ch[i]__; s->len=s->len+t->len; return(1); } else { if (pos+t->len<=MAXLEN) /*插入后串长>MAXLEN,但串t的字符序列可以全部插入*/ { for (i=MAXLEN-1;i>t->len+pos-1;i--)    s->ch[i]=s->ch[i-t->len]; for (i=0;i<t->len;i++)  ___s->ch[pos+i]=t->ch[i]; s->len=MAXLEN; } else /*插入后串长>MAXLEN,并且串t的部分字符也要舍弃*/ {  for (i=0;i<MAXLEN-pos;i++)  __s->ch[pos+i]=t->ch[i]__ ; s->len=MAXLEN; } return(1); } }   void main() { SString *str1; SString  *str2; int i,j,k,pos; int flag=0; str1 = (SString *)malloc(sizeof(SString)); str1->len = 0; printf("creat the string 1:\n"); createstring(str1); printf("creat the string 2:\n"); createstring(str2); printf("input the insert local:"); scanf("%d",&pos); flag=StrInsert(str1,pos,str2); if(flag == 0) printf("insert error!"); else { printf("after insert:\n"); output(str1); }  }