首页 新闻 会员 周边

这个程序那里有问题呢?为什么编译没有错,运行就出错呢?

0
[已关闭问题]

#include<stdio.h>
#define MaxSize 50
typedef struct
{
 char ch[MaxSize];
 int StrLength;
}SeqString;
void Assign(SeqString*s,char t[])//赋值
{
 for(int j=0;t[j]!='\0';j++)
  s->ch[j]=t[j];
        s->ch[j]=t[j];
 s->StrLength=j;
}
int Length(SeqString s)//求长度
{
 return(s.StrLength);
}
int Epual(SeqString s,SeqString t)//判断两个串是否相等
{
 if(s.StrLength!=t.StrLength)
  return(0);
for(int i=0;i<s.StrLength;i++)
if(s.ch[i]!=t.ch[i])
return(0);
return(1);
}
SeqString Concat(SeqString s,SeqString t)//串值的联接
{
 for(int i=0;i<t.StrLength;i++)
  s.ch[s.StrLength+i]=t.ch[i];
 s.ch[s.StrLength+t.StrLength]='\0';
 s.StrLength=s.StrLength+t.StrLength;
 return(s);
}
SeqString Substr(SeqString s,int i,int len)//求子串
{
 SeqString t;
 if(i<0||len<0||i+len-1>=s.StrLength)
 {
  t.ch[0]='\0';
  t.StrLength=0;
  return(t);
 }
 for(int k=i;k<i+len;k++)
  t.ch[k-i]=s.ch[k];
 t.ch[len]='\0';
 t.StrLength=len;
 return(t);
}
void Insert(SeqString *s,int i,SeqString t)//插入子串
{
 int k;
 if(i<0||i>s->StrLength)
  return;
 for(k=s->StrLength-1;k>=i;k--)
  s->ch[k+t.StrLength]=s->ch[k];
 for(k=i;k<i+t.StrLength;k++)
  s->ch[k]=t.ch[k-i];
 s->ch[s->StrLength+t.StrLength]='0';
  s->StrLength=s->StrLength+t.StrLength;

}
void Delete(SeqString *s,int i,int len)//删除子串
{
 if(i<0||i+len-1>=s->StrLength)
  printf("error");
 else{
  for(int k=i+len;k<s->StrLength;k++)
   s->ch[k-len]=s->ch[k];
   s->ch[s->StrLength-len]='\0';
s->StrLength=s->StrLength-len;
 }
}
void main()
{  
 SeqString s;
    //SeqString t;
 char t[50];
    printf("输入字符串:");
 gets(t);
 getchar();
 Assign(&s,&t[50]);
    Length(s);
 //printf("%s",str);
}

胖龙的主页 胖龙 | 初学一级 | 园豆:200
提问于:2010-05-12 22:52
< >
分享
其他回答(2)
0

我简单看了一下

Assign(&s,&t[50]); 这个地方就不对 t 这个数组的长度为50,而t[50] 已经访问到第51个元素去了,越界了。

从你的代码逻辑看,这里应该是想把 t 里面的字符串赋值给 s

应该用 StrCpy(&t[0], (char*)&s)

没有运行你的代码,写的不一定对。

以后发问题最好说明一下错误在哪一句,什么错误。

eaglet | 园豆:17139 (专家六级) | 2010-05-13 07:55
0

错误提示?

Astar | 园豆:40805 (高人七级) | 2010-05-13 08:38
0

把提示的错误信息加上。

代码苦行僧 | 园豆:311 (菜鸟二级) | 2010-05-13 12:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册