"strncpy_s" 不工作
"strncpy_s" Not Working
我正在尝试将 strncpy_s 用于从一个单词到一个数组的字符(我无法在 Visual Studio 2013 中使用 strncpy,而且我对 strncpy_s 完全陌生)。无论我做什么,我都会不断收到这些错误:
Error 1 error C2660: 'strncpy_s' : function does not take 3 arguments
Error 2 IntelliSense: no instance of overloaded function "strncpy_s"
matches the argument list argument types are: (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 stopped working"错误
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
的调用没有遵循 declaration of strncpy_s
,它列出了 四个 参数(如果第一个参数是 char *
就像你的情况一样):
errno_t strncpy_s(
char *strDest,
size_t numberOfElements,
const char *strSource,
size_t count
);
但更重要的是,您声明您希望在一个数组 first_array[]
中得到多个字符串,每个字符串都包含比上一个更短的输入字符串版本。 但是你声明的first_array[]
只包含一个char *
字符串,你初始化first_array[0]
的那个, 恰好是一个字符长(终止空字节):
char* first_array[] = {""};
即使你声明它持有5 char *
(初始化是没有必要的,因为你无论如何都要复制内容)...
char * first_array[5];
...您还没有为五个 char *
字符串中的每一个分配 内存 space。您只有五个指针指向任何地方,并且必须动态分配内存,取决于用户输入。
因为我还没讲过如果用户输入超过五个字符会发生什么,更不用说32了...
至此,即使我会post"working"代码,对你的教益也很少。您显然是在遵循某种教程,或者实际上是在尝试通过反复试验来学习。我认为这里的正确答案是:
获取不同的教程。更好的是,得到一个 good book on C or a good book on C++ 因为在线教程是出了名的缺乏。
我正在尝试将 strncpy_s 用于从一个单词到一个数组的字符(我无法在 Visual Studio 2013 中使用 strncpy,而且我对 strncpy_s 完全陌生)。无论我做什么,我都会不断收到这些错误:
Error 1 error C2660: 'strncpy_s' : function does not take 3 arguments
Error 2 IntelliSense: no instance of overloaded function "strncpy_s" matches the argument list argument types are: (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 stopped working"错误
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
的调用没有遵循 declaration of strncpy_s
,它列出了 四个 参数(如果第一个参数是 char *
就像你的情况一样):
errno_t strncpy_s(
char *strDest,
size_t numberOfElements,
const char *strSource,
size_t count
);
但更重要的是,您声明您希望在一个数组 first_array[]
中得到多个字符串,每个字符串都包含比上一个更短的输入字符串版本。 但是你声明的first_array[]
只包含一个char *
字符串,你初始化first_array[0]
的那个, 恰好是一个字符长(终止空字节):
char* first_array[] = {""};
即使你声明它持有5 char *
(初始化是没有必要的,因为你无论如何都要复制内容)...
char * first_array[5];
...您还没有为五个 char *
字符串中的每一个分配 内存 space。您只有五个指针指向任何地方,并且必须动态分配内存,取决于用户输入。
因为我还没讲过如果用户输入超过五个字符会发生什么,更不用说32了...
至此,即使我会post"working"代码,对你的教益也很少。您显然是在遵循某种教程,或者实际上是在尝试通过反复试验来学习。我认为这里的正确答案是:
获取不同的教程。更好的是,得到一个 good book on C or a good book on C++ 因为在线教程是出了名的缺乏。