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.

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).

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

xpi0t0s

23Jun2009 17:58

Re: Help! Segmentation fault

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.