tags:

views:

65

answers:

2
char *str = malloc (14);
sprintf(str, "%s", "one|two|three");

char *token1, *token2, *token3;
char *start = str;

token1 = str;
char *end = strchr (str, '|');
str = end + 1;
end = '\0';

token2 = str;
end = strchr (str, '|');
str = end + 1;
end = '\0';

...

free(start);

does that free work properly since I have been setting bytes within str to null in order to tokenize it?

+5  A: 

Yes it works, free does not care where the null termination is. Or even if there is one. You can use malloc/free for any type of data not only null terminated strings.

Brian R. Bondy
+3  A: 

the free doesn't check the contents of the data. So yes this is correct

Toad