-1

I was using the Decoder for Microsoft Script Encoder. It works perfectly well when I run it in Codeblocks. But when I run it in Visual Studio, it shows me the following errors
Snippet 1:

char decodeMnemonic(unsigned char *mnemonic)
{
    int i = 0;
    while (entities[i].entity != NULL)
    {
        **if (strcmp(entities[i].entity, mnemonic) == 0)**
       **//Error 1: cannot convert argument 2 from 'unsigned char *'
       // to 'const char *'**   
        return entities[i].mappedchar;
        i++;
    }
    printf("Warning: did not recognize HTML entity '%s'\n", mnemonic);
    return '?';
}

I had to integrate the Decoder in a program, so instead of passing the filenames as command line arguments, I have given their filepaths myself in the code.

Snippet 2:

    int main()
    {
        unsigned char *inname = "C:\\Users\\Karthi\\Desktop\\Project Winter 2018-19\\poweliks_sample\\poweliks_encoded_js.bin";
        unsigned char *outname = "C:\\Users\\Karthi\\Desktop\\Project Winter 2018-19\\poweliks_sample\\decoded1.txt";
        unsigned int cp = 0;
   //**Error 2: 'initializing': cannot convert from 'const char [87]' to 'unsigned char *'**    
Raisa A
  • 437
  • 7
  • 21
  • String literals are read only and therefore const. Use const in your type definitions. – Sami Kuhmonen Feb 26 '19 at 07:09
  • The second error is quite clear: The string literals are arrays of *constant* characters. They will decay to pointers to `const char`, i.e. `const char*`. – Some programmer dude Feb 26 '19 at 07:11
  • 1
    The first error is also quite clear, you have `unsigned char*` but the compiler wants `char*` (the `const` is less relevant in this case). So now the big question for you: *Why* do you use **`unsigned`** `char`? – Some programmer dude Feb 26 '19 at 07:13
  • @SamiKuhmonen String literals are "read-only" in C, in C++ they are actually constant to begin with (arrays of `const char`). – Some programmer dude Feb 26 '19 at 07:14

2 Answers2

2

You can use reinterpret_cast (for unsigned char* to const char*). But if you go from const unsigned char* to a non const type, you have to use const_cast first, since reinterpret_cast cannot cast away a const.

The paragraph below gives a brief overview, why your code did not work.

According to C99 Standard (similiar to other C standards), a string literal has static storage duration and its type is char[] The standard says:

If the program attempts to modify such an array, the behavior is undefined.

The reason why your program worked, when you used argv is, that argv is not considered as an array of string literals. This means you can modify them.

Simdi
  • 794
  • 4
  • 13
1

Here are the solutions for your problems:

Snippet 1: strcmp is a method to compare two C Strings. It expects const char* types.

int strcmp ( const char * str1, const char * str2 ); You have two option to solve it:

  1. Declare your method like this

    char decodeMnemonic(const char *mnemonic)
    
  2. Use C++Strings and declare your method like this

    char decodeMnemonic(std::string mnemonic)
    

If you use the second solutions you have to call the c_str()-Method to use it in strcmp

if (strcmp(entities[i].entity, mnemonic.c_str()) == 0)

Or you use only C++-String: read here how to use it: http://www.cplusplus.com/reference/string/string/compare/

Snippet 2: You can't use it like this because you have string literals which are arrays constant characters. Please use C++-Strings. You work with C++, so use his features (https://www.geeksforgeeks.org/stdstring-class-in-c/)

Anyway if you want to use it C-like: https://www.programiz.com/c-programming/c-strings

char c[] = "abcd";
char c[50] = "abcd";

Or with const (C++)

char *str1 = "string Literal";
const char *str2 = "string Literal";
Mosa
  • 373
  • 1
  • 14