注册 登录
编程论坛 数据结构与算法

子串定位在 VC 上可以运行但是在 OJ 上提交不了怎么破!!

Echo_87 发布于 2014-06-12 11:09, 783 次点击
代码是这样的
#include <stdio.h>
#define MaxSize 100
typedef struct  {
    char data[MaxSize];
    int len;
}SqString;  
void StrAssign(SqString &str,char c[]) {  
    int i;
    char a[MaxSize];
    gets(a);
    c=a;  
    for (i=0;c[i]!='\0';i++)
    str.data[i]=c[i];
    str.len=i;
}
int Index(SqString s,SqString t){
    int i=0,j=0;
    while(i<s.len&&j<t.len){
        if(s.data[i]==t.data[j]){
            i++;
            j++;
        }
        else{
            i=i-j+1; j=0;
        }
    }
    if(j>=t.len)
    return(i-t.len+1);
    else
    return 0;
}
void main()
{
    SqString s4,s5;
    char *l,*m;

    int w;
    StrAssign(s4,l);
    StrAssign(s5,m);
    Index(s4,s5);
    w=Index(s4,s5);
    if(w)  
        printf("%d\n",w);
    else  
    printf("Failed!\n");
}
我觉的问题应该出现在l和m上,但是不知道怎么改,求大神指教啊
4 回复
#2
azzbcc2014-06-12 19:00
错误提示什么?

目测你用了C++的引用,可能与这个有关
#3
Echo_872014-06-13 08:17
Segmentation fault:段错误,检查是否有数组越界,指针异常,访问到不应该访问的内存区域

但是我不知道那里出了问题啊
#4
Echo_872014-06-13 08:18
回复 2 楼 azzbcc
Segmentation fault:段错误,检查是否有数组越界,指针异常,访问到不应该访问的内存区域

但是我不知道那里出了问题啊
#5
azzbcc2014-06-13 15:45
你定义的 l m 完全没意义啊

或者说 StrAssign 的第二个参数没必要

其实应该把 gets 函数放到主函数中的

段错误原因没找到。
1