C strcpy() - evil?

前端 未结 17 1423
梦毁少年i
梦毁少年i 2020-12-23 10:50

Some people seem to think that C\'s strcpy() function is bad or evil. While I admit that it\'s usually better to use strncpy() in order to avoid bu

17条回答
  •  抹茶落季
    2020-12-23 11:01

    Your code is terribly inefficient because it runs through the string twice to copy it.

    Once in strlen().

    Then again in strcpy().

    And you don't check s1 for NULL.

    Storing the length in some additional variable costs you about nothing, while running through each and every string twice to copy it is a cardinal sin.

提交回复
热议问题