cc11tr24731

Dev C++ strtok_s throws [Warning] assignment makes pointer from integer without a cast


I have the following program:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) 
{
   char *tp = NULL, *cp = NULL, *next_token = NULL;
   char TokenListe[] = "Hello,I Am,1";

   tp = strtok_s(TokenListe, ", ", &next_token);

   printf(tp);
   return 0;
}

When I compile it with Visual Studio 2015 it compiles, without any warning. But when I compile it with Dev C++ 5.11 I get the following warning in line 10:

[Warning] assignment makes pointer from integer without a cast

Is there any solution to fix that warning?


Solution

  • Since C11, strtok_s is now standard C, part of the optional "bounds-checking interface" (Annex K). Compilers need not support it.

    But if they do, the format is this (C11 K.3.7.3.1):

    #define __STDC_WANT_LIB_EXT1__ 1
    #include <string.h>
    
    char *strtok_s(char * restrict s1,
                   rsize_t * restrict s1max,
                   const char * restrict s2,
                   char ** restrict ptr);
    

    Any other format is non-standard garbage and should not be used, including Microsoft strtok_s.

    Dev C++ is no longer maintained and therefore only contains a very old version of gcc. It does not support C11, but to my knowledge, no newer version of gcc + libraries yet support the C11 bounds-checking interface either. Visual Studio is a non-conforming compiler and can't be used for compiling standard C. Generally, I would advise to use neither of these compilers, but to update to a new version of gcc (for example Codeblocks with Mingw).

    Summary: strtok_s cannot be used in sensible ways. Use strtok instead. Simply ensure that all buffers involved are large enough and can't be overrun. In case of a multi-threaded program, simply don't use strtok at all.