2

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 1
    [`strtok_s`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strtok-s-strtok-s-l-wcstok-s-wcstok-s-l-mbstok-s-mbstok-s-l) in Visual studio is not the C11 standard [`strtok_s`](http://en.cppreference.com/w/c/string/byte/strtok) - they take different parameters, hence the warning. And in C++ you should not be using `char` arrays or C string functions – UnholySheep Mar 02 '18 at 08:18
  • Are you compiling as C++ or as C? If C, if Dev C++ doesn't have the non-standard strtok_s, it will be implicitly declared, and assumed to return integer. Do you get other errors, such as `implicit declaration of function 'strtok_s'`, or (when linking) `undefined reference to \`strtok_s'`? – Thomas Padron-McCarthy Mar 02 '18 at 08:20
  • I'm compiling as C –  Mar 02 '18 at 08:22

3 Answers3

4

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
1

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.

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
  • So I have declare it for myself, to avoid the warning? –  Mar 02 '18 at 08:28
  • If there is no strtok_s function, it doesn't help if you declare it. – Thomas Padron-McCarthy Mar 02 '18 at 08:29
  • If Dev C++ does contain an implementation of strtok_s, and links with it, then yes, declaring it will (probably) work. But a better option is probably to find the right header file, or compiler flags, to get it declared. – Thomas Padron-McCarthy Mar 02 '18 at 08:34
  • There _is_ a standard [`strtok_s`](http://en.cppreference.com/w/c/string/byte/strtok) function, but it differs from the Microsoft [`strtok_s`](https://msdn.microsoft.com/en-us/library/ftsafwz3.aspx) – Jabberwocky Mar 02 '18 at 08:37
  • 2
    Dev C++ is no longer maintained. They stopped maintaining it long before C11, so it doesn't conform to that standard. Neither does Visual Studio. – Lundin Mar 02 '18 at 08:51
0

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**);
  • This will make the warning go away, but will either cause linker errors, or cause your program to crash and burn. – Lundin Mar 02 '18 at 08:52
  • My programm doesnt crash. But is there any other solution? –  Mar 02 '18 at 08:55
  • 2
    @k.Lennartz if your program doesn't crash, it may be because you are "lucky". You have three clean options: 1. Don't use `strtok_s` but `strtok`. 2. Find an implementation of `strtok_s` (with google it shouldn't be difficult) and use _that one_. 3. Drop Dev C++ alltogether and use something different. – Jabberwocky Mar 02 '18 at 09:30