strstr() Runtime Error - HELP!
i have made this strstr() function,it gives the correct answer if it finds the correct string inside the another but the program crashes if it does not find the string,gives a runtime error,compliler does not give any error!
here is my code:
Code:
char *astrstr(const char *s1,const char *s2)
{
char *p=NULL;
int x=astrlen(s1);
int y=astrlen(s2);
int o=0,z=0;
for(int i=0;i<x; i++)
{
for(int k=0;k<y;k++)
{
if(s1[i]==s2[k])
{ o=0;z=0;
for(int l=i;l<x;l++)
{
if(s1[o+i]==s2[o])
{
z++;
}
o++;
}
}
if(z==y)
{
return (char *)(s1+i);
}
}
}
return p;
}
int _tmain(int argc, _TCHAR* argv[])
{
char s[]={"this is a simple string"};
char t[]={"is"};
cout << " S: "<< s << endl << " T: "<< t << endl << endl;
cout << "My Function: " << endl << astrstr(s,t) << endl << " String Function : " << endl << strstr(s,t) << endl << "S: "<< s << endl << "T: "<< t << endl;
return 0;
}
Re: strstr() Runtime Error - HELP!
Debug your code step by step to see where this error happens and what are the values of all your variables in this moment.
Re: strstr() Runtime Error - HELP!
One problem you have is in this section
Code:
for(int l=i;l<x;l++)
{
if(s1[o+i]==s2[o])
{
z++;
}
o++;
}
There's no constraint on o, so it walks right of the end of s2.
Re: strstr() Runtime Error - HELP!
i debugged my code and it gives the following error just as it calls my function
"Unhandled exception at 0x1026f8e0 in Assignment 3_Question 2.exe: 0xC0000005: Access violation reading location 0x00000000."
Re: strstr() Runtime Error - HELP!
Quote:
Originally Posted by
arshad115
i debugged my code and it gives the following error just as it calls my function
"Unhandled exception at 0x1026f8e0 in Assignment 3_Question 2.exe: 0xC0000005: Access violation reading location 0x00000000."
There's a little more to debugging than that. You need to look at the line of code that caused the error, what values are in the variables and when appropriate, the call stack. You can also watch your program run line by line, watching the variables as they change. That's what I did to see o running off the end of s2.
I showed you where I believe the error is coming from.
Re: strstr() Runtime Error - HELP!
A null pointer access probably wouldn't come from an array overrun. Both are problems, but probably different ones.
What's bugging me is the use of {} here:
Code:
char s[]={"this is a simple string"};
char t[]={"is"};
I'm not sure if it's actually wrong, but it is strange.
Re: strstr() Runtime Error - HELP!
Quote:
Originally Posted by
Lindley
A null pointer access probably wouldn't come from an array overrun. Both are problems, but probably different ones.
It's not crashing at the overrun, but it's symptomatic of faults in his logic that are causing him to set an invalid pointer downstream.
Re: strstr() Runtime Error - HELP!
Code:
char s[]={"this is a simple string"};
char t[]={"is"};
is used to to initialize the array by the string given,the comipler counts the number automatically
Re: strstr() Runtime Error - HELP!
Quote:
Originally Posted by
arshad115
Code:
char s[]={"this is a simple string"};
char t[]={"is"};
is used to to initialize the array by the string given,the comipler counts the number automatically
Then why do you use {}? :confused:
Code:
char s[] = "this is a simple string";
char t[] = "is";
Re: strstr() Runtime Error - HELP!
Normally you'd just write
Code:
char s[]="this is a simple string";
char t[]="is";