Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用字符数组调用scanf_s()_C++_Arrays_Scanf - Fatal编程技术网

C++ 使用字符数组调用scanf_s()

C++ 使用字符数组调用scanf_s(),c++,arrays,scanf,C++,Arrays,Scanf,我试过这个代码,但不起作用 char word1[40]; printf("Enter text: \n"); scanf_s("%s", word1); printf("word1 = %s", word1); 当我执行它时,它显示: 如果仔细阅读,您会注意到必须提供字符串缓冲区的长度: 与scanf和wscanf不同,scanf_和wscanf_需要缓冲区大小 为c、c、s、s或字符串类型的所有输入参数指定 包含在[]中的控件集。缓冲区大小(以字符为单位)为 作为指向的指针后面的附加参数传

我试过这个代码,但不起作用

char word1[40];
printf("Enter text: \n");
scanf_s("%s", word1);
printf("word1 = %s", word1);
当我执行它时,它显示:

如果仔细阅读,您会注意到必须提供字符串缓冲区的长度:

与scanf和wscanf不同,scanf_和wscanf_需要缓冲区大小 为c、c、s、s或字符串类型的所有输入参数指定 包含在[]中的控件集。缓冲区大小(以字符为单位)为 作为指向的指针后面的附加参数传递 缓冲区或变量

调整您的
scanf_s()
调用,如下所示:

scanf_s("%s", word1, _countof(word1));
这应该行得通


(注意,代码> > CONTROP()/CUT>需要包含<代码> <代码>。< /P>它听起来可能很愚蠢,但是当它提示时,你输入了什么东西吗?如果你在C++中工作,你一般应该使用<代码> STD::String 和IoFielsI/Endos/In String。

scanf_s("%s", word1, _countof(word1));