为什么不让我用strncpy?这报错是什么意思?这程序该怎么写?
问题描述:
/* 2.cpp .2. 编写一个函数,
把字符串中所有的字符都前移一个位置,
而串中的第一个字符移到最后。并编写主程序进行测试。*/
#include <iostream>
#include <string.h>
#include <cstring>
using namespace std;
//#1.返回前移后的字符串
/*
string move(string s)
{
}
*/
//#2.字符串数组作为函数参数
void move(char* s)
{
int n = strlen(s);
char* str = new char[n];
strncpy(str, s, n - 1);
strncpy(str, s, 1);
strncpy(s, str, n);
delete[]str;
}
/*string转换成char的方法
const char* pstr=str.c_str();
*/
int main()
{
string s;
cin >> s;
const char *str = s.c_str();
move(str);
cout << str << endl;
return 0;
}
答
两种方法:
1.用 strncpy_s函数代替strncpy函数
2.右键你的工程名,选择属性->c/c++->预处理器->预处理器定义中加入_CRT_SECURE_NO_WARNINGS