strtok () Another token function .

1
2
3
4
5
6
7
8
9
10
11
12
13
 
  // Pass the string to be tokenized and get the first token. 
  tok = strtok(str, delims); 
 
  // Get all remaining tokens. 
  while(tok) { 
    cout << tok << endl; 
 
    // Each subsequent call to strtok() is passed NULL 
    // for the first argument. 
    tok  = strtok(NULL, delims); 
  } 
 


Maybe it's an effect of returning by reference, but I still don't understand.

tok = strtok(str, delims);

tok = strtok(NULL, delims);

How can strtok knows that NULL means to continue in the str string ?
IIRC, it has a static char* internally stored that it continues to use if you pass NULL; otherwise it sets that variable to the one you pass.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#define MAX_TOK_SIZE 128 
const char *gettoken(const char *str) { 
  static char token[MAX_TOK_SIZE+1]; 
  static const char *ptr; 
  int count; // holds the current character count 
  char *tokptr; 
 
  if(str) { 
    ptr = str; 
  } 
 
  tokptr = token; 
  count = 0; 
 
  while(isspace(*ptr)) ptr++; 
 
  if(isalpha(*ptr)) { 
    while(isalpha(*ptr) || isdigit(*ptr)) { 
      *tokptr++ = *ptr++; 
      ++count; 
      if(count == MAX_TOK_SIZE) break; 
    } 
  } else if(isdigit(*ptr)) { 
    while(isdigit(*ptr)) { 
      *tokptr++ = *ptr++; 
      ++count; 
      if(count == MAX_TOK_SIZE) break; 
    } 
  } else if(ispunct(*ptr)) { 
    *tokptr++ = *ptr++; 
  } else return NULL; 
 
  // Null-terminate the token. 
  *tokptr = '\0'; 
 
  return token;  
}


If so,please help me to understand this :

1.
#define MAX_TOK_SIZE 128

is the same as const MAX_TOK_SIZE = 128;

2. The static token is declared inside the function and will have effect if we use again,means that token can't get deleted and is unchanged and usable next time ?
1. No. #define is agnostic to syntax and scoping, whereas a const is not.
2. exactly.
Topic archived. No new replies allowed.