pointer increment error

This is a discussion on pointer increment error within the C++ Programming forums, part of the General Programming Boards category; can someone tell me what's wrong with this line of code pls? Code: while(*buffer != "

Thread: pointer increment error

"){cout << static_cast<unsigned int>(*buffer); *buffer++;} ...

  1. #1
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    pointer increment error

    can someone tell me what's wrong with this line of code pls?
    Code:
    while(*buffer != "\0"){cout << static_cast<unsigned int>(*buffer); *buffer++;}
    I keep getting the error:
    28 C:\Dev-Cpp\MyProjs\fencrypt.cpp ISO C++ forbids comparison between pointer and integer
    buffer is declared such:
    Code:
    char buffer[90];
    thanks
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,259
    The complaint is caused by the while-condition (*buffer != "\0"). *buffer is a char (an integral type). "\0" is a string, which the compiler sees as an array of char and (when doing comparisons) arrays are sometimes threated like they're pointers.

    Even though it is not resulting in a complaint from the compiler, the body of the loop is highly suspect as well: even more so if you apply the obvious fix (make the two things being compared of the same type) so the compiler does not complain about about the while-condition.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Make "\0" a character literal: '\0'.

    Then, get a separate pointer for traversing, because you can't increment an array.

    Once you've done that, you might as well use a for loop, which is more suitable for this kind of task.

    Code:
    for(const char *p = buffer; *p != '\0'; ++p) {
      std::cout << static_cast<unsigned int>(*p);
    }
    There's several more ways to accomplish this. But this is the simplest.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I got around it my own way thanks
    I now have a new problem. I output the converted string into a file, close the file then re-open the file with ios::in.
    Even though the string is stored as an interger cast I can't cast back to the ascii form:
    Code:
    while (!b_file.eof()) {
    char i;
           i = static_cast<char>(b_file.get() );
           cout << i;
          
    }
    still only outputs numbers, not the original string.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    It's kinda silly to store all those chars as integers in the file (assuming you did, as you did cast the mess).
    AFAIK, istream.get returns a reference to itself, so your code is invalid.
    Further, using istream to read anything other than strings, might bite you later too. From my own experiments, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You wrote the text serialization of the integer into the file, then you read individual bytes back and cast their numeric form to char - and you expect something even remotely similar to come out?

    Oh, and about "while (!b_file.eof())", read the FAQ.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Oh, and about "while (!b_file.eof())", read the FAQ
    can't find anything in the FAQ related to it. Also could you explain what text serialisation is pls??
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by WDT View Post
    can't find anything in the FAQ related to it.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21