Pelles C forum

C language => Beginner questions => Topic started by: LaVanTien on April 19, 2015, 07:10:40 AM

Title: How to turn on strtok_r function
Post by: LaVanTien on April 19, 2015, 07:10:40 AM
Hi! I'm learning C.
I want to use strtok_r instead of strtok, but I can't use it although I included "string.h".
How to active this function? I've tried define __STDC_WANT_LIB_EXT1__ 1 but not success.
#define __STDC_WANT_LIB_EXT1__ 1
#include<stdio.h>
#include<string.h>

int main(void) {
char s[]="I'm going to eat a snack hihi";
char *k;
while(!(char *p=strtok_r(s," ",&k)))
puts(s);
getchar();
return 0;
}

/*
Error:
Building test.obj.
C:\Users\Admin\Documents\Pelles C Projects\test\test.c(8): error #2160: Extraneous identifier 'p'.
C:\Users\Admin\Documents\Pelles C Projects\test\test.c(8): error #2001: Syntax error: expected ')' but found '='.
C:\Users\Admin\Documents\Pelles C Projects\test\test.c(8): error #2039: Invalid expression.
C:\Users\Admin\Documents\Pelles C Projects\test\test.c(8): error #2001: Syntax error: expected ')' but found 'strtok_r'.
C:\Users\Admin\Documents\Pelles C Projects\test\test.c(8): warning #2018: Undeclared function 'strtok_r' (did you mean 'strtok_s'?); assuming 'extern' returning 'int'.
C:\Users\Admin\Documents\Pelles C Projects\test\test.c(8): error #2001: Syntax error: expected ';' but found ')'.
C:\Users\Admin\Documents\Pelles C Projects\test\test.c(8): error #2061: Invalid statement termination.
*** Error code: 1 ***
Done.
*/


Another question that when I build above code it give me a ton of errors, I think that I can declare a variable inside a loop at it has allowed since C99, but when I declare a variable inside while loop I get some errors, can someone explain why? Thanks for reading my first post in this box. 
Title: Re: How to turn on strtok_r function
Post by: Snowman on April 19, 2015, 03:50:45 PM
The function strtok_r() comes from POSIX (which lives in Unix, mostly).
The Windows equivalent, according to this StackOverflow thread (http://stackoverflow.com/questions/9021502/whats-the-difference-between-strtok-r-and-strtok-s-in-c), is strtok_s().

So simply use strtok_s() instead of strtok_r().