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 != "
"){cout << static_cast<unsigned int>(*buffer); *buffer++;}
...
-
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: thanks
A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside
-
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.
-
Cat without Hat
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
-
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
-
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.

Originally Posted by
Adak
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.

Originally Posted by
Salem
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.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
-
Cat without Hat
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
-
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
-

Originally Posted by
WDT
can't find anything in the FAQ related to it.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351

Originally Posted by
Adak
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.

Originally Posted by
Salem
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.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Popular pages Recent additions
Similar Threads
-
By Moony in forum C Programming
Replies: 0
Last Post: 03-20-2008, 07:59 AM
-
By markiz in forum Windows Programming
Replies: 31
Last Post: 03-17-2008, 02:39 PM
-
By Shamino in forum C++ Programming
Replies: 2
Last Post: 06-10-2007, 11:54 AM
-
By Diod in forum C++ Programming
Replies: 8
Last Post: 02-13-2006, 09:33 AM
-
By Trent_Easton in forum Windows Programming
Replies: 8
Last Post: 07-15-2005, 10:52 PM