0

I have never quite been able to wrap my head around pointers, strings, chars etc. I need help with this error. Here is the code snippet...

string H = "פטיש";
string G = "Σφυρί";

bool Interphase(int argc, char * args[]); //DLL import from Analytical.a

char * Hcopy = new char[H.length() + 1]; 
std::strcpy(Hcopy, H.c_str());
char * Gcopy = new char[G.length() + 1]; 
std::strcpy(Gcopy, G.c_str());

while (Interphase(147, Hcopy) == true || Interphase(148, Gcopy) == true)//C2664 here!
    {// Do stuff...}

Please note the code is altered to reflect the error only.

Also how to compile in Visual Studio 2012 Ultimate without

Warning C4566: character represented by universal-character-name '\u05E9' cannot be represented in the current code page (1252)

Thanks.

pzaenger
  • 11,381
  • 3
  • 45
  • 46
loumbut5
  • 65
  • 8
  • Warnings are helpful! Don't ignore them! The warning you are trying to remove is a helpful warning: `"פטיש"` can't be represented as a `char` array. This will most likely give you wrong results. – Rakete1111 Sep 05 '16 at 13:39
  • 1
    Why are you using pointers and C-strings at all? This is 2016, not 1972! Why not simply call `Interphase(147, &H[0])` and `Interphase(148, &G[0])`? (Are you sure those `argc` arguments are correct?) – Lightness Races in Orbit Sep 05 '16 at 13:41
  • @LightnessRacesinOrbit Wikipedia C++: "First appeared 1983" OK. – cadaniluk Sep 05 '16 at 13:45
  • 1
    @Downvoter: Pointers and C-strings are idioms from C, which was invented in 1972. They're not idiomatic C++ (which, as we know it today, came about in 1998 actually). That's the point. – Lightness Races in Orbit Sep 05 '16 at 13:46
  • @LightnessRacesinOrbit: If you use the standardisation as starting point, to be fair, C started in 1990 (1989 in the US). However, they are still the way to go **in C** - which (I assume) is your actual point :-) – too honest for this site Sep 05 '16 at 14:02

1 Answers1

0

Your error:

Error C2664: 'Strategy::Interphase' : cannot convert parameter 2 from 'char *' to 'char *[]'

means that you're giving a char* to the function which expects a char*[].

bool Interphase(int argc, char * args[]);

Note the 2nd argument, it's a pointer to an array (or array of arrays, or pointer to pointer).

Your code can be modified to give it the address of the pointer:

while (Interphase(1, &Hcopy) == true || Interphase(1, &Gcopy) == true)
    // ...

but I doubt that's how the API is meant to be used.

I'm not familiar with that function, but I'm guessing it's used more like:

const char** args = {"arg1", "arg2"};
Interphase(2, args);

Your warning:

Warning C4566: character represented by universal-character-name '\u05E9' cannot be represented in the current code page (1252)

means that char can't hold non-ASCII characters.

If you're doing Unicode, you must use wide characters and strings (wchar_t and wstring) to allow bigger numbers. Otherwise, you'll have an overflow and likely a wrap.

Link to comparison between string and wstring.


As a side note, you really shouldn't use C-style strings (char*). Use std::string instead. The reason:

  • Need to deallocate them yourself (your code leaks memory as-is)
  • Unsafe api (needing to pass the size along with the data)
Community
  • 1
  • 1
Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48
  • 1
    Actually `std::string`s store Unicode code points encoded in UTF-8. – Bob__ Sep 05 '16 at 13:59
  • 2
    @Bob__: Total nonsense. `std::string` stores bytes, and has no inherent encoding whatsoever. – Lightness Races in Orbit Sep 05 '16 at 14:09
  • @Ivan So my question would be how to pass strings into the Interphase(); function in the dll. Also changing from string to wstring only changes the error to Error C2664: 'Strategy::Interphase' : cannot convert parameter 2 from 'wchar_t *' to 'char *[]' – loumbut5 Sep 05 '16 at 14:21
  • @loumbut5 If the function takes a `char` then you can't expect it to work with non-ASCII strings like "פטיש". My answer shows an example of how to use the function. – Ivan Rubinson Sep 05 '16 at 14:25
  • @LightnessRacesinOrbit: Well, the real nonsense IMHO is that `std::string` is unaware of its encoding, but yes, it stores bytes. In some environments the byte sequence is natively encoded in UTF-8. – Bob__ Sep 05 '16 at 16:06
  • @Bob__: On the contrary, it's a good thing because you can use whatever encoding you like on top. – Lightness Races in Orbit Sep 05 '16 at 16:07
  • @LightnessRacesinOrbit: That's fine, but you should be able to know how it is encoded. In a simple way. – Bob__ Sep 05 '16 at 16:12
  • @Bob__: Yes, your project documentation states that information, as well as surrounding code comments around the string's declaration. Right? – Lightness Races in Orbit Sep 05 '16 at 16:13
  • @LightnessRacesinOrbit: Yeah (not really, I'm afraid), but the Standard Library is unaware of the encoding too. To sort a string like `"Olé"` with `std::sort`, for example, I have to convert it to UTF-32 or UTF-16 before. I find it an inconvenience (IMHO, again). – Bob__ Sep 05 '16 at 16:41
  • @Bob__: It's not an inconvenience. It is a convenience. If everybody in the entire world were forced to use an encoding just because Bob__ wanted to use it. Can you imagine! Instead you are free to encode your string over your bytes however you like, using the appropriate library. – Lightness Races in Orbit Sep 05 '16 at 16:45
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122687/discussion-between-bob-and-lightness-races-in-orbit). – Bob__ Sep 05 '16 at 16:50
  • Ok, one error down. Thanks @Ivan. Now how to solve Error C2196: case value '?' already used. The code goes like case 'א': if (/*Conditional*/) {/*Do stuff*/} – loumbut5 Sep 06 '16 at 08:36