【发布时间】:2015-02-17 13:54:16
【问题描述】:
我正在尝试使用 strncpy_s 将字符从一个单词转换为数组(我无法在 Visual Studio 2013 中使用 strncpy,而且我对 strncpy_s 完全陌生)。无论我做什么,我都会不断收到这些错误:
错误 1 错误 C2660: 'strncpy_s' : 函数不接受 3 个参数
错误 2 IntelliSense:没有重载函数“strncpy_s”的实例 匹配参数列表的参数类型有:(char *, char, int)
我的代码的目的是:
如果用户输入,例如“HELLO”(即 text = HELLO) 然后->
Copy HELLO to first_array [0] Copy ELLO to first_array [1] Copy LLO to first_array [2] Copy LO to first_array [3] Copy O to first_array [4]
这是我的代码:
int _tmain(int argc, _TCHAR* argv[])
{
char text[32];
cin >> text;
char* first_array[] = {""};
int n = strlen(text);
for (int i = 0; i < n; i++)
{
strncpy_s(first_array[i], text[i], n-i);
}
}
编辑 1。再修改一下代码,现在程序可以运行了,但是输入一个文本后,它突然给我“example.exe停止工作”的错误。
int _tmain(int argc, _TCHAR* argv[])
{
char* text[32];
cin >> *text;
char* first_array[] = {""};
//int n = strlen(text);
int n = sizeof(text);
for (int i = 0; i < n; i++)
{
strncpy_s(first_array[i], n - i, text[i], 32);
}
【问题讨论】:
-
'strncpy_s' : function does not take 3 arguments... 你的答案就在那里。您传递的参数are: (char *, char, int)。这是三个论点,这是错误的。查看 remyabel 的链接,这实际上是您在搜索引擎中输入“strncpy_s”时的热门链接。 -
如果这确实是 C++(因为它最初被标记),那么为什么不使用
std::string和朋友呢? -
缺少#include。 @DevSolar 公平地说,Microsoft 还记录了仅 C++ 的 3 参数版本。
-
为什么不能使用
strncpy?并不是说它会比strncpy_s更好,只是一种不同的坏。无论如何,first_array是char*的一元数组,所以无论哪种方式,情况都同样糟糕。另外,avoidTCHAR.