C++ strcpy 와 strcpy_s 질문 드립니다
-
게시물 수정 , 삭제는 로그인 필요
c++ 언어 초보입니다. 윤성우 열혈 c++ 책을 보며 독학중입니다.
예제
#include <iostream>
#include <cstring>
using namespace std;
namespace CAR_CONST
{
enum
{
ID_LEN=20, MAX_SPD=200, FUEL_STEP=2, ACC_STEP=10, BRK_STEP=10
};
}
class car
{
private:
char gamerID[CAR_CONST::ID_LEN];
int fuelgauge;
int curspeed;
public:
void initmembers(const char* ID, int fuel);
void showcarstate();
void Accel();
void Break();
};
void car::initmembers(const char* ID, int fuel)
{
strcpy(gamerID, ID);
fuelgauge = fuel;
curspeed = 0;
}
void car::showcarstate()
{
cout << "소유자ID: " << gamerID << endl;
cout << "연료량: " << fuelgauge << "%" << endl;
cout << "현재속도: " << curspeed << "km/s" << endl << endl;
}
void car::Accel()
{
if (fuelgauge <= 0)
return;
else
fuelgauge -= CAR_CONST::FUEL_STEP;
if ((curspeed + CAR_CONST::ACC_STEP) >= CAR_CONST::MAX_SPD)
{
curspeed = CAR_CONST::MAX_SPD;
return;
}
curspeed += CAR_CONST::ACC_STEP;
}
void car::Break()
{
if (curspeed < CAR_CONST::BRK_STEP)
{
curspeed = 0;
return;
}
curspeed -= CAR_CONST::BRK_STEP;
}
int main(void)
{
car run99;
run99.initmembers("run99", 100);
run99.Accel();
run99.Accel();
run99.Accel();
run99.showcarstate();
run99.Break();
run99.showcarstate();
return 0;
}
여기서 책에는 strcpy 라고 써있는데 오류가 떠서
찾아보니 strcpy_s 를 사용하여 실행하니 정상적으로 결과값이 나오게 됩니다.
strcpy 와 strcpy_s 차이가 정확히 무엇인가요?
#c++ strcpy #c++ strcpy_s #c++ strcpy 오류 #c++ strcpy_s 사용법 #c++ strcpy 사용법 #c++ strcpy strcpy_s #c++ strcpy 구현 #c++ strcpy_s 헤더 #c++ strcpy string #c++ strcpy header