strcpy 及其第二个参数错误
当我尝试编译该程序时,我收到有关 strcpy 的第二个参数的错误(包含在代码下方)。老实说,我对如何解决这个问题感到困惑。如果我的代码效率不高或不美观,我很抱歉;我只是一名刚开始学CS的学生。
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
int main(){
int r = 0;
char *article[]={"the", "a", "one", "some", "any"};
char *noun[]={"boy","girl","dog","town","car"};
char *verb[]={"drove","jumped","ran","walked","skipped"};
char *preposition[]={"to","from","over","under","on"};
char sentence [80];
srand(time(NULL));
for(int i=0;i<=20;i++){
r = (rand()%5);
strcpy(sentence,*article[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*noun[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*verb[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*preposition[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*article[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*noun[r]);
strcat(sentence,".");
}
sentence[0]= toupper(sentence[0]);
cout<<sentence <<endl;
system("pause");
return 0;}
1>Compiling...
1>assignment 8.cpp
1>e:\assignment 8\assignment 8\assignment 8.cpp(16) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>e:\assignment 8\assignment 8\assignment 8.cpp(20) : error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(23) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(26) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(29) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(32) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(35) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
When I try and compile this program, I get errors (included below the code) about strcpy's second argument. I'm honestly stumped on what to do to fix it. And I'm sorry if my code is not efficient or pretty to look at; I'm just a beginning CS student.
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
int main(){
int r = 0;
char *article[]={"the", "a", "one", "some", "any"};
char *noun[]={"boy","girl","dog","town","car"};
char *verb[]={"drove","jumped","ran","walked","skipped"};
char *preposition[]={"to","from","over","under","on"};
char sentence [80];
srand(time(NULL));
for(int i=0;i<=20;i++){
r = (rand()%5);
strcpy(sentence,*article[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*noun[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*verb[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*preposition[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*article[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*noun[r]);
strcat(sentence,".");
}
sentence[0]= toupper(sentence[0]);
cout<<sentence <<endl;
system("pause");
return 0;}
1>Compiling...
1>assignment 8.cpp
1>e:\assignment 8\assignment 8\assignment 8.cpp(16) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>e:\assignment 8\assignment 8\assignment 8.cpp(20) : error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(23) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(26) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(29) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(32) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(35) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
危险。
strcat()
和strcpy()
是代码癌症的主要原因。使用它们会使您面临各种缓冲区溢出问题。使用strncat()
/strncpy()
,或者(更好)只使用std::string
,因为您使用的是 C++!strcat()
和strcpy()
期望它们的参数是字符串。*article[r]
是单个char
--article[r]
是您想要的字符串。因此,删除前导星号。DANGER.
strcat()
ANDstrcpy()
ARE THE LEADING CAUSES OF CODE CANCER. Using them exposes you to all kinds of buffer overflows. Usestrncat()
/strncpy()
, or (even better) just usestd::string
, since you're using C++!strcat()
andstrcpy()
expect their arguments to be strings.*article[r]
is a singlechar
--article[r]
is the string you want. So, drop the leading asterisks.您的星号太多了 -
noun[r]
已经为您提供了一个char*
,因此您无需再添加一个*
第二个参数。此外,strcat 是一个不安全的函数,如果您的缓冲区(在您的情况下是句子)对于内容来说太小,则可能会意外崩溃您的程序。
请改用
strncat
- 您需要向该函数添加一个参数,即缓冲区大小 - 在本例中为80
。然后,如果缓冲区不足而不是程序崩溃,您只会注意到您的句子在末尾被剪掉。You have one asterisk too many -
noun[r]
already gives you achar*
so you don't need to put an additional*
in the second parameter.Also,
strcat
is an unsafe function and can crash your program unexpectedly if your buffer (in your case,sentence
) is ever too small for the content.Please use
strncat
instead - you'll need to add one more parameter to that function, which is the buffer size - in this case,80
. Then in case of undersized buffer instead of program crash you would just notice that your sentence is clipped at the end.您的文章、名词和动词是 char 指针数组。当选择数组中要使用的项目时,您将获得一个 char* 来表示要使用的单词。这个 char* 是 strcpy 所期望的 - 当您取消引用 char* (即 article[r])时,您最终会得到一个 char,而不是 char。
此外,strcpy 是一个不安全的字符串运算符,因此它可以覆盖大量内存或以其他方式打开安全漏洞。有什么原因不允许您使用 std::string 来完成此作业吗?
Your articles, nouns, and verbs are arrays of char pointers. When selecting the item in the array to use, you get a char* to the word to use. This char* is what strcpy expects - when you dereference the char* (i.e. article[r]), you end up with a char, not a char.
Also, strcpy is an unsafe string operator, so it can overwrite large clumps of memory or otherwise open gaping security holes. Is there any reason you're not allowed to use std::string for this assignment?
过多的取消引用,例如更改:
到
其他实例,类似地。
Too much de-referencing, e.g. change:
to
and similarly for the other instances.
*article[r]
是char
类型的值。它是字符串的第一个字符。strcpy
需要字符串的地址,即article[r]
。*article[r]
is a value of typechar
. It's the first character of the string.strcpy
expects the address of the string which is simplyarticle[r]
.而不是
你想要的
Instead of
you want