我为什么要敲代码 2016-07-14 04:07 采纳率: 0%
浏览 1304

C++ 串赋值时出现分段错误,怎么回事

运行程序时,在串赋值时,老出现 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;

}

  • 写回答

1条回答

  • shiter 人工智能领域优质创作者 2016-07-14 04:53
    关注

    段错误?用什么未经初始化的指针了?

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?