0

I am trying to use libxml2 with libiconv to parse xml files. Now I faced encoding conversion issue.

here is my function:

char* convert_iconv(char* in)
{
    char* out;
    size_t size, out_size;

    size = strlen(in) + 1;
    out_size = size * 2 - 1;
    out = (char*)malloc(out_size);

    iconv_t ic = iconv_open("WINDOWS-1251", "UTF-8");
    iconv(ic, &in, &size, &out, &out_size);
    iconv_close(ic);

    
    return (out);
}

function iconv(ic, &in, &size, &out, &out_size) gives an error:

cannot convert argument 2 from 'char **' to 'const char **'

okay, that function really needs const char **

//size_t iconv(iconv_t cd, const char** inbuf, size_t * inbytesleft, char** outbuf, size_t * outbytesleft);

But any attempts to pass const char ** as an argument fail. First error still occurs, and another error appears:

argument of type "const char **" is incompatible with parameter of type "char **"

Changing

char* convert_iconv(char* in)

to

char* convert_iconv(const char* in)

doesn't help

Can you please tell me what I am doing wrong?

273K
  • 29,503
  • 10
  • 41
  • 64
Kirumata
  • 165
  • 1
  • 9
  • Is this C or C++? – tadman Jul 28 '22 at 13:20
  • 1
    I see that `iconv` takes `char **` for both parameters. – Sam Varshavchik Jul 28 '22 at 13:21
  • my program is c++, but library is implemented in C https://gitlab.gnome.org/GNOME/libxml2 – Kirumata Jul 28 '22 at 13:22
  • 3
    What does "any attempts to pass const char **" mean? Show the code, don't describe it. – Sam Varshavchik Jul 28 '22 at 13:26
  • The `in` parameter for the function should be `const char*`, and you should leave the type of `out` alone. There is also no need to cast string literals to `const char*`; that's the implicit conversion and its only effect is to hide type problems. – molbdnilo Jul 28 '22 at 13:49
  • Change `char* in` to `const char* in`. Also, remove all four of those dangerous and unnecessary casts. One is even incorrect – ikegami Jul 28 '22 at 14:04
  • I tried to change the type of parameter `in` from `char *` to `const char *` (added that in question) and got both of mentioned errors. Also I understood about tree unnecessary casts (integer and string literals). But what's wrong with `malloc`? – Kirumata Jul 28 '22 at 14:25
  • 1
    ***But what's wrong with malloc*** [https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-and-or-new](https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-and-or-new) – drescherjm Jul 28 '22 at 15:11

0 Answers0