strcpy() error in Visual studio 2012

后端 未结 7 935
Happy的楠姐
Happy的楠姐 2021-02-06 12:50

so I have this code:

#include \"stdafx.h\"
#include 
#include 
#include 
using namespace std;

...<

7条回答
  •  生来不讨喜
    2021-02-06 13:07

    The message you are getting is advice from MS that they recommend that you do not use the standard strcpy function. Their motivation in this is that it is easy to misuse in bad ways (and the compiler generally can't detect and warn you about such misuse). In your post, you are doing exactly that. You can get rid of the message by telling the compiler to not give you that advice. The serious error in your code would remain, however.

    You are creating a buffer with room for 10 chars. You are then stuffing 11 chars into it. (Remember the terminating '\0'?) You have taken a box with exactly enough room for 10 eggs and tried to jam 11 eggs into it. What does that get you? Not doing this is your responsibility and the compiler will generally not detect such things.

    You have tagged this C++ and included string. I do not know your motivation for using strcpy, but if you use std::string instead of C style strings, you will get boxes that expand to accommodate what you stuff in them.

提交回复
热议问题