strcpy及其第二个参数出错

时间:2010-11-11 19:41:44

标签: c++ 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

6 个答案:

答案 0 :(得分:6)

<强>危险。 strcat()strcpy()是代码癌症的主要原因。使用它们会使您暴露于各种缓冲区溢出。使用strncat() / strncpy()或(甚至更好)只使用std::string,因为您使用的是C ++!

strcat()strcpy()期望他们的参数是字符串。 *article[r]是一个char - article[r]是您想要的字符串。所以,放下主要的星号。

答案 1 :(得分:2)

您有一个星号太多 - noun[r]已经为您提供了char*,因此您无需在第二个参数中添加额外的*

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

请改为使用strncat - 您需要再向该函数添加一个参数,即缓冲区大小 - 在本例中为80。然后,如果缓冲区过小而不是程序崩溃,你只会注意到你的句子在最后被剪裁了。

答案 2 :(得分:2)

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

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

答案 3 :(得分:1)

过多的解除引用,例如改变:

strcpy(sentence,*article[r]);

strcpy(sentence, article[r]);

和其他情况类似。

答案 4 :(得分:1)

*article[r]char类型的值。这是字符串的第一个字符。 strcpy期望字符串的地址只是article[r]

答案 5 :(得分:1)

而不是

 strcpy(sentence,*article[r]);

你想要

 strcpy(sentence,article[r]);