【问题标题】:Error with strcpy and its second argumentstrcpy 及其第二个参数出错
【发布时间】:2010-11-11 19:41:43
【问题描述】:

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

【问题讨论】:

  • 为什么不使用 std::string?
  • 标记为作业。如果不是,请随时删除标签。
  • 这也不是家庭作业。是我试图用我在课堂上学到的东西来做一个程序

标签: c++ strcpy


【解决方案1】:

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

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

【讨论】:

  • 感谢您的帮助。使用这么多 strcpy 和 strcat 的原因是因为这是我们正在学习的,所以我不得不在这个程序中使用它。
  • 告诉你的教授或老师他在教巫术。邪恶的邪恶编程巫术。你永远不应该永远使用strcat()strcpy()——即使是在枪口下!
【解决方案2】:

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

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

请改用strncat - 您需要向该函数添加一个参数,即缓冲区大小 - 在本例中为80。然后在缓冲区过小而不是程序崩溃的情况下,您只会注意到您的句子在结尾处被剪辑。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      取消引用过多,例如改变:

      strcpy(sentence,*article[r]);
      

      strcpy(sentence, article[r]);
      

      对于其他实例也是如此。

      【讨论】:

        【解决方案5】:

        *article[r]char 类型的值。它是字符串的第一个字符。 strcpy 需要字符串的地址,即 article[r]

        【讨论】:

          【解决方案6】:

          代替

           strcpy(sentence,*article[r]);
          

          你想要的

           strcpy(sentence,article[r]);
          

          【讨论】:

            猜你喜欢
            • 2018-08-20
            • 2013-04-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-22
            • 2018-06-26
            • 1970-01-01
            相关资源
            最近更新 更多