I am having trouble figuring out where and why I am getting a segmentation error.
I am writing C code that prompts the user to enter a regular expression and compile it, and then enter a line with several sentences:
int main(void){ char RegExp[50]; regex_t CompiledRegExp; char *para; char delim[] = ".!?,"; char *sentence; char *ptr1; printf("Enter regular expression: "); fgets(RegExp, 50, stdin); if (regcomp(&CompiledRegExp,RegExp,REG_EXTENDED|REG_NOSUB) != 0) { printf("ERROR: Something wrong in the regular expression\n"); exit(EXIT_FAILURE); } printf("\nEnter string: ");
strtok_r is used to split the string into any of the following delimiters.,?! and then the received token (sentence) is used as a string parameter in the regexec function, which searches for it to see if the previously expressed regular expression contains the token:
if( fgets(para, 1000, stdin)){ char *ptr = para; sentence = strtok_r(ptr, delim, &ptr1); while(sentence != NULL){ printf("\n%s", sentence); if (regexec(&CompiledRegExp,sentence,(size_t)0,NULL,0) == 0) { printf("\nYes"); } else { printf("\nNo"); } ptr = ptr1; sentence = strtok_r(ptr, delim, &ptr1); } } regfree(&CompiledRegExp); }
This is probably the stupid mistake I am making, but any help in identifying the reasons for segfaul would be greatly appreciated!
EDIT: Moved regfree to a more appropriate location. However, segfault is still happening. I am sure this is due to how the regular expression is read or how it is compared in regexec . Incredible, however.