#include<string.h>
#include<stdio.h>
int firstState(char s[], int length);
int secondState(char s[], int length);
int thirdState(char s[], int length);
int forthState(char s[], int length);
int main()
{
char string[10];
gets(string);
if( firstState(string, 0) )
printf("Accept\n");
else
printf( "Not accept\n" );
return 0;
}
int firstState(char s[], int length)
{
if(s[length] == 'a')
return (secondState(s, length++));
else if(s[length] == 'b')
return firstState(s, length++);
else
return 0;
}
int secondState(char s[], int length)
{
if(s[length] == 'a')
return secondState(s, length++);
else if(s[length] == 'b')
return thirdState(s, length++);
else
return 0;
}
int thirdState(char s[], int length)
{
if(s[length] == 'a')
return secondState(s, length++);
else if(s[length] == 'b')
return forthState(s, length++);
else
return 0;
}
int forthState(char s[], int length)
{
if(s[length] == 'a')
return secondState(s, length++);
else if(s[length] == 'b')
return firstState(s, length++);
else
return 0;
}
It gave me a segmentation fault or core dumped I’m confused!!!
can someone explain why It gave me this kind of bug????
and tell how to debug to make my code run very clearly!!
I’m really tired with this 🙁
sorry for my bad English
Segmentation Fault is an access violation, i.e. when the program tries to access emmory it’s not supposed to http://en.wikipedia.org/wiki/Segmentation_fault.
In your case it’s caused by unsafe indexing into array, e.g.
You don’t actually check if new length is still within bounds, so if the string wasn’t null-terminated, which it probably isn’t in your case, then you’ll have an infinite loop resulting in SegFault.
For debugging, using a GUI would be the most sane approach, try Visual Studio on Windows, Eclipse on everything else.