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

376 Views Asked by At

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?

3

There are 3 best solutions below

0
Lundin On BEST ANSWER

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.

5
Thomas Padron-McCarthy On

If Dev C++ doesn't have the non-standard strtok_s, in C it will be implicitly declared, and assumed to return integer.

Note: strtok_s is in the standard, but as an "optional extension", according to (my free draft copy of the) C11 standard.

You should enable other warnings too, such as the warning for implicit declarations of functions.

If Dev C++ does contain an implementation of strtok_s, and links with it, declaring it yourself might work. But a better option is to find the right header file, or compiler flags, to get it declared, if any such options exist. Consult the documentation.

But note, as Michael Walz commented, that the strtok_s in the C11 standard and Microsoft's strtok_s are different, and don't have the same parameters! I don't know which version Dev C++ implements.

3
AudioBubble On

Based on the answer from @thomas-padron-mccarthy, I could fix my problem with declaring the strtok_s function in my header file.

extern char* strtok_s(char*, char*, char**);