我为什么要敲代码 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 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。