strcat Vs strncat - When should which function be used?

后端 未结 5 852
无人及你
无人及你 2021-01-02 11:55

Some static code analyzer tools are suggesting that all strcat usage should be replaced with strncat for safety purpose?

In a program, if we know clearly the size of

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-02 12:19

    They don't do the same thing so they can't be substituted for one another. Both have different data models.

    • A string for strcat is a null terminated string for which you (as the programmer) guarantee that it has enough space.
    • A string for strncat is a sequence of char that is either terminated at the length you are indicating or by a null termination if it is supposed to be shorter than that length.

    So the use of these functions just depends on the assumptions that you may (or want to) do about your data.

提交回复
热议问题