3

I have problem converting next code from c to c++:
I have a function that takes an array of move sequences (sequence of chars from a to i) as 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:
main.cpp:17:39: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

I believe this warning comes from the fact that string in C is defined as char[], while c++ uses std::string.
So I tried replacing the code to use C++ strings like this:

void mkmove(std::string* move) {

in mkmove function defenition, and:

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

in main function and add the C++ string header file:

#include <string>

but now I get errors:
main.cpp: In function ‘void mkmove(std::string*)’:
main.cpp:11:21: error: cannot convert ‘std::string {aka std::basic_string}’ to ‘char*’ in assignment
main.cpp: In function ‘int main()’:
main.cpp:19:39: error: scalar object ‘movestr’ requires one element in initializer

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++?

Thanks for answers!

-Slovenia1337

3 Answers 3

5

use

std::string movestr[] = {"abde", "abc", "bcef", "adg", "bdefh", "cfi", "degh", "ghi", "efhi"};
Sign up to request clarification or add additional context in comments.

1 Comment

Or (using C++11) std::vector<std::string> movestr = {"abde", "abc", ... };
5

No, the warning isn't because you should use string, it's because character strings are read-only.

Either declare strings as char ...[] or as const char *.

In your case, you're declaring an array of char *, which is a deprecated feature (converting from const char * to char *.

3 Comments

+1 for explaining what the error really means, but it's probably best to switch from char* to std::string instead of char const *
Absolutely. But if he's converting C code to C++, it'll be orders of magnitude harder to switch everything over to string, this'll just make his life that much easier.
I guess which way is best depends on how much has to be converted. If the above code is actually the whole program I'd switch over to C++ types. (Of course its obviously not a whole program, or if it is it doesn't do anything)
0

I believe this warning comes from the fact that string in C is defined as char[], while c++ uses std::string.

No, in C string literals are constant char[], while in C++ they are to const char[].

Fix the const-correctness of your program thus:

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.