[英]pointer to char array C++
I have problem converting next code from c to c++: 我在将下一个代码从c转换为c ++时遇到问题:
I have a function that takes an array of move sequences (sequence of chars from a to i) as arg. 我有一个函数,将一组移动序列(从a到i的字符序列)作为arg。
code: 码:
void mkmove(char** move) {
int i = 0;
char* p = NULL;
int moves[9][9];
for(int i = 0; i < 9; i++) {
for(p = move[i]; *p; p++) {
moves[i][*p - 'a'] = 3;
}
}
}
int main() {
char* movestr[] = {"abde", "abc", "bcef", "adg", "bdefh", "cfi", "degh", "ghi", "efhi"};
mkmove(movestr);
return(0);
}
gcc compiles that code fine, but if I try to compile it with g++ it gives me next warning: gcc可以很好地编译该代码,但是如果我尝试使用g ++进行编译,则会发出下一个警告:
main.cpp:17:39: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings] main.cpp:17:39:警告:不建议将字符串常量转换为'char *'[-Wwrite-strings]
I believe this warning comes from the fact that string in C is defined as char[], while c++ uses std::string. 我相信此警告来自以下事实:C中的字符串定义为char [],而c ++使用std :: string。
So I tried replacing the code to use C++ strings like this: 因此,我尝试替换代码以使用C ++字符串,如下所示:
void mkmove(std::string* move) {
in mkmove function defenition, and: 在mkmove函数定义中,以及:
std::string* movestr = {'abde', "abc", "bcef", "adg", "bdefh", "cfi", "degh", "ghi", "efhi"};
in main function and add the C++ string header file: 在主函数中并添加C ++字符串头文件:
#include <string>
but now I get errors: 但是现在我得到了错误:
main.cpp: In function 'void mkmove(std::string*)': main.cpp:在函数'void mkmove(std :: string *)'中:
main.cpp:11:21: error: cannot convert 'std::string {aka std::basic_string}' to 'char*' in assignment main.cpp:11:21:错误:无法在分配中将“ std :: string {aka std :: basic_string}”转换为“ char *”
main.cpp: In function 'int main()': main.cpp:在函数'int main()'中:
main.cpp:19:39: error: scalar object 'movestr' requires one element in initializer main.cpp:19:39:错误:标量对象“ movestr”在初始化程序中需要一个元素
I also tried some other tweaks but that gave me least errors on compile. 我还尝试了其他一些调整,但这使编译时的错误最少。
So what is proper way to convert top code from C to C++? 那么将顶级代码从C转换为C ++的正确方法是什么?
Thanks for answers! 感谢您的回答!
-Slovenia1337 -斯洛文尼亚1337
使用
std::string movestr[] = {"abde", "abc", "bcef", "adg", "bdefh", "cfi", "degh", "ghi", "efhi"};
No, the warning isn't because you should use string
, it's because character strings are read-only. 不,警告不是因为您应该使用string
,而是因为字符串是只读的。
Either declare strings as char ...[]
or as const char *
. 将字符串声明为char ...[]
或const char *
。
In your case, you're declaring an array of char *
, which is a deprecated feature (converting from const char *
to char *
. 就您而言,您要声明一个char *
数组,这是一个已弃用的功能(从const char *
转换为char *
。
I believe this warning comes from the fact that string in C is defined as
char[]
, while c++ usesstd::string
. 我认为此警告来自以下事实:C中的字符串定义为char[]
,而c ++使用std::string
。
No, in C string literals are constant char[]
, while in C++ they are to const char[]
. 不,在C字符串文字中,常量为char[]
,而在C ++中,它们为const char[]
。
Fix the const-correctness of your program thus: 通过以下方式修复程序的const正确性:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.