Here is the code:

#include "stdafx.h" #include <string.h> #include <string.h> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { char *s = "eeee eee ."; char t[99], *sa[20]; int c, n = 0; sa[n] = strtok_s(t, " ,.\n", NULL); while (sa[n]) sa[++n] = strtok_s(NULL, " ,.\n", NULL); for (c = 0; c < n; ++c) printf("%s\n", sa[c]); return 0; } 

terminates the program. I can not fix it. Where is the mistake?

Wednesday - MS Visual C ++ 2012.

  • Perhaps because s is a literal that resides in static storage and cannot be changed. Make it not char *, but char [] - Viktor Kozlov
  • It still stops working. char s [] = "eeee eee."; char t [99], * sa [20]; int c, n = 0; sa [n] = strtok_s (s, ",. \ n", NULL); while (sa [n]) {sa [++ n] = strtok_s (NULL, ",. \ n", NULL); } for (c = 0; c <n; ++ c) printf ("% s \ n", sa [c]); return 0; - Arthur352
  • In general, the third argument should not be NULL, but a string. - Viktor Kozlov
  • gives an error message. already tried everything. the same mistake. I copy the code from the site, I run it myself - it does not work. What is the mistake and do not know close - Arthur 352

1 answer 1

@Arthur352 , I think the error is that the third argument should be the address of the pointer , and you have NULL.

I write in Linux, here instead of the Windows strtok_s() there is a similar function strtok_r() .

Here is an example with her.

 avp@avp-ubu1:~/hashcode$ cat strtok.c #include <stdio.h> #include <stdlib.h> #include <string.h> int main (int ac, char *av[]) { char *token, *saveptr, *str = av[1] ? *(++av) : (char *)"xaxa xoxo xixi", *strd = strdup(str); const char *sep = av[1] ? av[1] : " "; int i = 0; str = strd; printf ("source: [%s] separators: [%s]\n", str, sep); while (token = strtok_r(str, sep, &saveptr)) { printf ("token[%d] : <%s>\n", i++, token); str = 0; } free(strd); return puts("End") == EOF; } avp@avp-ubu1:~/hashcode$ g++ strtok.c avp@avp-ubu1:~/hashcode$ ./a.out source: [xaxa xoxo xixi] separators: [ ] token[0] : <xaxa> token[1] : <xoxo> token[2] : <xixi> End avp@avp-ubu1:~/hashcode$ ./a.out " zqwe 223 , sy, ,sa,so , ," ", " source: [ zqwe 223 , sy, ,sa,so , ,] separators: [, ] token[0] : <zqwe> token[1] : <223> token[2] : <sy> token[3] : <sa> token[4] : <so> End avp@avp-ubu1:~/hashcode$ echo $? 0 avp@avp-ubu1:~/hashcode$ 

What is not clear, ask