|
-
March 10th, 2009, 09:15 AM
#1
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;
}
Last edited by arshad115; March 10th, 2009 at 09:18 AM.
Reason: forgot the code tags!...;p
-
March 10th, 2009, 09:25 AM
#2
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.
Victor Nijegorodov
-
March 10th, 2009, 09:28 AM
#3
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.
-
March 10th, 2009, 09:43 AM
#4
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."
-
March 10th, 2009, 09:48 AM
#5
Re: strstr() Runtime Error - HELP!
 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.
-
March 10th, 2009, 10:35 AM
#6
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.
Last edited by Lindley; March 10th, 2009 at 10:38 AM.
-
March 10th, 2009, 11:24 AM
#7
Re: strstr() Runtime Error - HELP!
 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.
-
March 10th, 2009, 11:54 AM
#8
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
-
March 10th, 2009, 11:58 AM
#9
Re: strstr() Runtime Error - HELP!
 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 {}? 
Code:
char s[] = "this is a simple string";
char t[] = "is";
Victor Nijegorodov
-
March 10th, 2009, 11:59 AM
#10
Re: strstr() Runtime Error - HELP!
Normally you'd just write
Code:
char s[]="this is a simple string";
char t[]="is";
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|