简体   繁体   English

无法将 'char (*)[200]' 转换为 'char**'

[英]Cannot Convert 'char (*)[200]' to 'char**'

#include <iostream>
#include <string.h>

using namespace std;

void ArrayTimesThree(char*, const char*);

int main()
{
    char s1[200], s2[200], circleword[200];
    cin.getline(s1, 200);
    cin.getline(s2, 200);

    ArrayTimesThree(circleword, s1);
    cout<<circleword[1];
}

void ArrayTimesThree(char *dest[], char *source[])
{
    *dest[0] = NULL;
    strcat(*dest, *source);
    strcat(*dest, *source);
    strcat(*dest, *source);
}

main.cpp|21|error: cannot convert 'char (*)[200]' to 'char**' for argument '1' to 'void ArrayTimesThree(char**, char**)' main.cpp|21|错误:无法将参数 '1' 的 'char (*)[200]' 转换为 'char**' 到 'void ArrayTimesThree(char**, char**)'

You're passing ArrayTimesThree a char*, however, in the method signature you're telling it to expect a char**.您正在向 ArrayTimesThree 传递一个 char*,但是,在方法签名中,您告诉它期待一个 char**。 Don't forget that that using the [] operator counts as a dereference.不要忘记使用[]运算符算作取消引用。 Try this:尝试这个:

#include <iostream>
#include <string.h>

using namespace std;

void ArrayTimesThree(char*, char*);

int main()
{
    char s1[200], s2[200], circleword[200];
    cin.getline(s1, 200);
    cin.getline(s2, 200);

    ArrayTimesThree(circleword, s1);
    cout<<circleword[1];

    return 0;
}

void ArrayTimesThree(char *dest, char source[])
{
    dest[0] = '\0';
    strcat(dest, source);
    strcat(dest, source);
    strcat(dest, source);
}

Disclaimer: I'm not sure what exactly you're expecting out of this code, so I cannot guarantee the logic is correct;免责声明:我不确定您对这段代码的期望是什么,所以我不能保证逻辑是正确的; however, this will take care of your compiler errors and seems to function correctly for how the code is written.但是,这将解决您的编译器错误,并且似乎 function 正确地了解了代码的编写方式。

The problem is really just because your initial declaration of ArrayTimesThree (which is the 'correct' one) doesn't match the definition you later give (which is wrong, in fact).问题实际上只是因为您对ArrayTimesThree的初始声明(这是“正确的”)与您稍后给出的定义不匹配(实际上这是错误的)。 Change your definition as below and it works:如下更改您的定义,它可以工作:

void ArrayTimesThree(char* dest, const char* source) // Needs to be the same as in the previous declaration!
{
    dest[0] = '\0';   // Don't assign a string pointer to NULL! Instead, set its first character to the nul character
//  strcpy(dest, ""); // ALternatively, use strcpy with an empty string to clear "dest"
    strcat(dest, source); // strcat takes char* and const char* arguments ...
    strcat(dest, source); // ... so there is no need to 'deference the values ...
    strcat(dest, source); // ... now that the argument types have been 'corrected'
}

Incidentally, I notice that the input value for s2 in your main function is never actually used … is this what you intend, for now?顺便说一句,我注意到您的main function 中s2的输入值从未实际使用过……这是您现在想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM