0

当我尝试编译这个程序时,我收到关于 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
4

6 回答 6

6

危险。strcat()并且strcpy()是导致代码癌的主要原因。使用它们会使您面临各种缓冲区溢出。使用strncat()/ strncpy(),或者(甚至更好)只使用std::string,因为您使用的是 C++!

strcat()strcpy()期望他们的论点是字符串。*article[r]是一个单一的char--article[r]是你想要的字符串。所以,去掉前面的星号。

于 2010-11-11T19:45:36.237 回答
2

你有一个星号太多 -noun[r]已经给你一个char*所以你不需要*在第二个参数中添加一个额外的。

此外,这是一个不安全的功能,如果您的缓冲区(在您的情况下, )对于内容来说太小strcat,可能会意外地使您的程序崩溃。sentence

strncat改用 - 您需要向该函数添加一个参数,即缓冲区大小 - 在这种情况下,80. 然后,如果缓冲区大小不足而不是程序崩溃,您只会注意到您的句子在最后被剪掉了。

于 2010-11-11T19:46:10.480 回答
2

您的冠词、名词和动词是 char 指针数组。在选择要使用的数组中的项目时,您会得到一个 char* 来表示要使用的单词。这个 char* 是 strcpy 所期望的 - 当您取消引用 char* (即article[r])时,您最终会得到一个 char,而不是 char

此外,strcpy 是一个不安全的字符串运算符,因此它可以覆盖大量内存或以其他方式打开巨大的安全漏洞。是否有任何理由不允许您使用 std::string 进行此分配?

于 2010-11-11T19:47:44.770 回答
1

过多的取消引用,例如更改:

strcpy(sentence,*article[r]);

strcpy(sentence, article[r]);

对于其他情况也是如此。

于 2010-11-11T19:46:22.603 回答
1

*article[r]是一个类型的值char。它是字符串的第一个字符。strcpy期望字符串的地址是简单的article[r].

于 2010-11-11T19:46:57.580 回答
1

代替

 strcpy(sentence,*article[r]);

你要

 strcpy(sentence,article[r]);
于 2010-11-11T19:47:21.183 回答