Hi Iam getting a segmentation fault in the following program, it would be great if you can suggest where exactly iam going wrong, here Iam trying to extract substrings from a string and store it in a array of strings, please note the recursive call in the function. Code: #include<stdio.h> #include<string.h> #include<time.h> #include<stdlib.h> int k=0; int p=1; void parser(char in_str[][25],char out_str[][25]); int main() { char inputexp[500]; char label[50][25]; printf("enter expression"); scanf("%s",inputexp); strcpy(label[0],inputexp); parser(label,label); return 0; getchar(); getchar(); } void parser(char in_str[][25],char out_str[][25]) { while(in_str[k]!= '\0') { int len=strlen(in_str[k]); int openbrac_count=0,closebrac_count=0; int i=0; int from=0; while(i<len) { while(i<len && in_str[k][i]!=')') { if((in_str[k][i]=='(')) { openbrac_count++; } i++; } while(i<len && in_str[k][i]!='(') { if(in_str[k][i]==')') { closebrac_count++; } if(openbrac_count==closebrac_count & openbrac_count != 0) { strncpy(out_str[k+1],&in_str[k][from],i-from); p=p+1; if (in_str[k][i+1]=='&' || in_str[k][i+1]=='|') { from=i+2; } } i++; } if (in_str[k][0]=='G') { strncpy(out_str[p+1],&in_str[k][2],strlen(in_str[k])-2); parser(in_str,out_str); } if (in_str[k][0]=='F') { strncpy(out_str[p+1],&in_str[k][2],strlen(in_str[k])-2); parser(in_str,out_str); } if (in_str[k][0]=='X') { strncpy(out_str[p+1],&in_str[k][2],strlen(in_str[k])-2); parser(in_str,out_str); } } k++; } }
Bitwise AND? Code: if(openbrac_count==closebrac_count & openbrac_count != 0) Gave it the input (a(b)) and the strncpy copied (a(b) with NO terminating NULL - is that intended? I guess not, because after k++ and the second time through the outer while, strlen returns some silly number. What input did you give and what result did you expect (i.e. contents of label)? Fixed the formatting so here it is for everyone else's benefit (with a couple of other small changes). Code: int main() { char inputexp[500]; char label[50][25]; printf("enter expression"); fgets(inputexp,498,stdin); // scanf("%s",inputexp); strcpy(label[0],inputexp); parser(label,label); return 0; } void parser(char in_str[][25],char out_str[][25]) { while(in_str[k]!= '\0') { int len=strlen(in_str[k]); int openbrac_count=0,closebrac_count=0; int i=0; int from=0; while(i<len) { while(i<len && in_str[k][i]!=')') { if((in_str[k][i]=='(')) { openbrac_count++; } i++; } while(i<len && in_str[k][i]!='(') { if(in_str[k][i]==')') { closebrac_count++; } if(openbrac_count==closebrac_count && openbrac_count != 0) { strncpy(out_str[k+1],&in_str[k][from],i-from); p=p+1; if (in_str[k][i+1]=='&' || in_str[k][i+1]=='|') { from=i+2; } } i++; } if (in_str[k][0]=='G') { strncpy(out_str[p+1],&in_str[k][2],strlen(in_str[k])-2); parser(in_str,out_str); } if (in_str[k][0]=='F') { strncpy(out_str[p+1],&in_str[k][2],strlen(in_str[k])-2); parser(in_str,out_str); } if (in_str[k][0]=='X') { strncpy(out_str[p+1],&in_str[k][2],strlen(in_str[k])-2); parser(in_str,out_str); } } k++; } }
Hi xpi0t0s, Thanks a lot for your help, this is the way i need my output array of strings to be like, For example a( b( c( d ) ) ) my substring array would be a( b( c( d ) ) ) b( c( d ) ) c( d ) d However said that the strings may not be as simple in the sense that it may also be as a( b( c( d ) ) ) ^ e(f(g))) in which case my substring set would be: a( b( c( d ) ) ) ^ e(f(g))) a( b( c( d ) ) ) b( c( d ) ) c( d ) d e(f(g))) f(g) g Another constraint is that i can also have a(b(a(c))) i.e 'a' repeting more than once ( it is for this i have tried to use recursion) for the above case my substrings would be: a(b(a(c))) b(a(c)) a(c) c yes it was and operator, i ran the code after making those corrections however i still get the segmentation fault. please help
Did you fix the problem with strncpy not appending a null? What I do in cases like this ideally is to step through the program with a debugger, but if I can't do that then I add a load of printf statements to say what the program is doing at any point. This kind of logging can be very helpful in tracking down a problem, for example if you can see that it has copied, say, (a(b) when you expected it to copy (a(b)) then you can look more closely that the code that does that to try to figure out where it is going wrong.