Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 指向字符数组C++;_C++_C_Arrays_Pointers - Fatal编程技术网

C++ 指向字符数组C++;

C++ 指向字符数组C++;,c++,c,arrays,pointers,C++,C,Arrays,Pointers,我在将下一个代码从c转换为c++时遇到问题: 我有一个函数,它将移动序列数组(从a到I的字符序列)作为arg 代码: 在mkmove函数中,以及: std::string* movestr = {'abde', "abc", "bcef", "adg", "bdefh", "cfi", "degh", "ghi", "efhi"}; 在主函数中添加p> > C++字符串头文件: #include <string> #包括 但现在我得到了错误: main.cpp:在函数“void

我在将下一个代码从c转换为c++时遇到问题:
我有一个函数,它将移动序列数组(从a到I的字符序列)作为arg

代码:

在mkmove函数中,以及:

std::string* movestr = {'abde', "abc", "bcef", "adg", "bdefh", "cfi", "degh", "ghi", "efhi"};
在主函数中添加p> > C++字符串头文件:

#include <string>
#包括
但现在我得到了错误:
main.cpp:在函数“void mkmove(std::string*)”中:
main.cpp:11:21:错误:无法在赋值中将'std::string{aka std::basic_string}'转换为'char*' main.cpp:在函数“int main()”中:
main.cpp:19:39:错误:标量对象“movestr”需要初始值设定项中的一个元素

我还尝试了一些其他的调整,但这给了我最少的编译错误

<> P~(上)代码从C到C++ +< /P>的正确方法是什么? 谢谢你的回答

-斯洛文尼亚1337使用

std::string movestr[] = {"abde", "abc", "bcef", "adg", "bdefh", "cfi", "degh", "ghi", "efhi"};

不,警告不是因为您应该使用
string
,而是因为字符串是只读的

将字符串声明为
char…[]
const char*

在您的例子中,您正在声明一个
char*
数组,这是一个不推荐使用的功能(从
const char*
转换为
char*

<> P>我相信这个警告来自C中的字符串定义为char []/Cudio>,而C++使用<>代码> STD::String >

<>不,在C字符串中,文本是常数<代码> char [] /Cuff>,而C++中的代码是<代码> const char []/COD> .< 通过以下方式修复程序的常量正确性:

void mkmove(const char** move) {
    const char* p;
    int moves[9][9];

    for(int i = 0; i < 9; i++) {
        for(p = move[i]; *p; p++) {
            moves[i][*p - 'a'] = 3;
        }
    }
}

int main() {
    const char* movestr[] = {"abde", "abc", "bcef", "adg", "bdefh", "cfi", "degh", "ghi", "efhi"};
    mkmove(movestr);

    return(0);
}
void mkmove(常量字符**move){
常量字符*p;
国际移动[9][9];
对于(int i=0;i<9;i++){
for(p=move[i];*p;p++){
移动[i][*p-'a']=3;
}
}
}
int main(){
const char*movestr[]={“abde”、“abc”、“bcef”、“adg”、“bdefh”、“cfi”、“degh”、“ghi”、“efhi”};
mkmove(movestr);
返回(0);
}

1 +,用于解释错误的真正含义,但最好是从<代码> char */COD>转换为<代码> STD::String 而不是< Case> char const */Cord>。但是如果他将C代码转换成C++,则将更难切换到字符串,这将使他的生命变得如此之多。我想哪种方式最好,取决于需要转换多少。如果上面的代码实际上是整个程序,我会切换到C++类型。(当然它显然不是一个完整的程序,或者如果它没有做任何事情)或者(使用C++ 11)<代码> STD::向量MOVESTR={ABDE”,“ABC”,…};< /C>
std::string movestr[] = {"abde", "abc", "bcef", "adg", "bdefh", "cfi", "degh", "ghi", "efhi"};
void mkmove(const char** move) {
    const char* p;
    int moves[9][9];

    for(int i = 0; i < 9; i++) {
        for(p = move[i]; *p; p++) {
            moves[i][*p - 'a'] = 3;
        }
    }
}

int main() {
    const char* movestr[] = {"abde", "abc", "bcef", "adg", "bdefh", "cfi", "degh", "ghi", "efhi"};
    mkmove(movestr);

    return(0);
}