What's the difference between strtok_r and strtok_s in C?

前端 未结 6 1166
刺人心
刺人心 2020-12-15 04:45

I\'m trying to use this function in a C program that needs to be able to compile in Linux and Windows. At first I tried using strtok_r, but then when I compiled on windows,

6条回答
  •  难免孤独
    2020-12-15 05:31

    strtok_s is simply the Windows version of strtok_r which is standard everywhere else.

    One (common I would think) way to make a program portable when it comes to functions like strtok_s/strtok_r is to use the preprocessor:

    #if defined(_WIN32) || defined(_WIN64)
    /* We are on Windows */
    # define strtok_r strtok_s
    #endif
    

    As the prototypes and functionality is the same, you can now use only strtok_r.

提交回复
热议问题