March 3rd, 2002, 12:44 PM
problem with strtok in a loop
here I have some code which is assigning tokens taken from a string with strtok to an array called hops. my problem is that once the loop has gone through the array, it's supposed to go on and do the same with another string.
However, as soon as the loop finishes and it reaches the last number in the string (in this case 17)...the program crashes.
anyone have any ideas? longstring has been read in from a text file if that's any use.
/*longstring looks like: -host-1-2-3-4-5-6-7-.....-16-17-*/
strcpy(buffer, strtok(longstring, "-"));
printf("%s\n", buffer); /*prints host*/
if(0 == strcmp(buffer,"host"))
while(strcpy(hops[i], strtok(NULL, "-"))!=NULL)
printf("%s\n", hops[i]); /*prints the digit*/
March 3rd, 2002, 12:57 PM
i think on the last run you get a line like
strcpy(hops[i],NULL) which causes a segfault since you donīt try to copy an empty string but a null-pointer.
split your loop, you should see it in a debugger then...
March 3rd, 2002, 03:31 PM
thanks a million...that sorted the problem out for me:p