运行程序时,在串赋值时,老出现 Program received signal SIGSEGV, Segmentation fault怎么回事。
代码如下,定义了一个串,在一个串中查找另一个串,采用无回溯的匹配算法:
#include
#include
#define maxsize 50
using namespace std;
typedef struct
{
char ch[50];
int len;
} CHARS;
void getnext(CHARS *S,int next[])
{
int j,k;
j=1;
k=0;
next[1]=0;
while(jlen)
{
if((k==0)||(S->ch[j-1]==S->ch[k-1]))
{
k++;
j++;
next[j]=k;
}
else
k=next[k];
}
}
int index(CHARS *S,CHARS *T,int next[])
{
int j,i;
j=1;
i=1;
while((i<=S->len)&&(j<=T->len))
{
if((j==0)||(S->ch[i-1]==T->ch[j-1]))
{
i++;
j++;
}
else j=next[j];
}
if(j>T->len)
return (i-T->len);
else
return 0;
}
int main()
{
CHARS *S,*T;
int ind;
void getnext(CHARS *S,int next[]);
int index(CHARS *S,CHARS *T,int next[]);
cout<<"please input a string :"<
cin>>S->ch;
S->len=strlen(S->ch);
cout<<"the string is : "<ch;
cout<<"please input the string wanted equry: "<
cin>>T->ch;
cout<<"the T -> ch is :"<ch<
T->len=strlen(T->ch);
int next[T->len];
getnext(S, next);
ind=index(S,T,next);
return 0;
}