> char* s[]
Pretty sure the problem is the type of variable s is. strtok works, in part, by searching for and replacing characters in the delimiter string with '\0' and then returning a pointer to the start of the string. Since this requires that you change the string variable, it needs to be stored in writeable memory. So s would become something like:
Code:
#define S_MAX 100
char s[S_MAX][4] = { ... };
The reason that an array of char pointers won't work is because the memory that they point to is actually stored in the read-only section of the executeable. To get writeable memory, you have to tell the compiler to use an array of arrays.
Well, you could also use an alternative method that doesn't try to change s.
Code:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char* s[] = { "12, 34, 56, 78", "82.16, 41.296",
"2, -3, 5, -7, 11, -13, 17, -19",
"9.00009, 90.0009, 900.009, 9000.09, 90000.9" };
int i;
for (i = 0; i < sizeof(s) / sizeof(s[0]); i++) {
char *token = s[i];
int pos = strcspn(token, ", ");
while (1) {
printf("%.*s\n", pos, token);
token += pos;
pos = strspn(token, ", ");
/* move past delimiters */
token += pos;
/* check for end of string */
if (*token == '\0') {
break;
}
pos = strcspn(token, ", ");
}
}
return 0;
}