0

I'm iterating over a char* as follows, but on the line indicated, I'm getting error C2446 '!=' no conversion from char* to int.

        int errorLineNumber = 0;
        char* json;  //this is a long json with multiple \n's in it.
        int offset = errorOffset;
        char* p = json + offset;
        while (*p-- != '\n' && offset--); //check that this gives offset on that error line and not char position of end of last line before error
        errorLineOffset = errorOffset - offset; //get offset on error line only

        //count lines to json error
        long errorLineNumber = 0;
        while (*p!= json) //this is error line
            errorLineNumber += *p-- == '\n';

I looked at conversion const char to tchar, but it's a little different, and I also looked at conversion const char to int, which I still don't think is the same issue, unless I'm missing something.

That would be great if someone had a good idea what I'm missing. Thanks!

Community
  • 1
  • 1
Michele
  • 3,617
  • 12
  • 47
  • 81
  • 1
    Show the types of the variables involved. – Pete Becker Apr 07 '17 at 14:19
  • Works fine in VS2015 – alexeykuzmin0 Apr 07 '17 at 14:21
  • 1
    Don't you mean while (p != json)? – jwimberley Apr 07 '17 at 14:24
  • 2
    It's a good idea to include a [mcve]. There's one person requesting more info, one who had it work for however they completed the shown code, and one who took a guess at the problem. None of these would be necessary with an MCVE. – chris Apr 07 '17 at 14:26
  • Which line is giving the error? It is not clear from the comments in the code. You also have several variables that you don't show types for (e.g. `errorLineOffset`, `errorOffset`). Please help us help you by expanding your code to be a [mcve]. – Rob K Apr 07 '17 at 14:53
  • Sorry, I added error line comment. I thought I had added that. – Michele Apr 07 '17 at 14:56
  • Also, you should be using modern C++ features, like std::strings and iterators. This code is too clever by half and takes way too much effort to understand it. – Rob K Apr 07 '17 at 18:31
  • I know. I used to have something more understandable, but my team asked me to change it to this. – Michele Apr 07 '17 at 21:29

1 Answers1

1

In line

while (*p != json) 

you compare a *p which is of type char to json, which, according to the code above should be a pointer, I assume it is of type `const char*. So you should do

while (p != json) ... 
Gert Wollny
  • 529
  • 2
  • 8