-
const char to int
im working on code so that you will be able to input code w/comments and this function will take them out after i compile i get a const char to int error, i think i know what this error is, im trying to convert a char into an int, i know c fairly well and this is my first jump to .cpp...i was just looking for some input on how to get this right. Thanks
Code:
void FilterComments (const char *in, char *out)
{
const char *inPTR;
char *outPTR;
inPTR = in;
outPTR = out;
while((inPTR) != '\0') /* while inPTR != null*/
{
if ((inPTR) == '/' || (inPTR) == '\')
{
inPTR++;
if ((inPTR) == '/' || (inPTR)== '*')
{
outPTR = ' ';
outPTR++;
}
else
{
if(inPTR == '"')
{
do{
inPTR++;
}
while((inPTR) != '"');
}
else
{
outPTR = inPTR;
inPTR++;
outPTR++;
}
}
}
else
{
if(inPTR == '"')
{
do{
inPTR++;
}
while((inPTR) != '"');
}
else
{
outPTR = inPTR;
inPTR++;
outPTR++;
}
}
}
}
-
What line is it on?
I'm sure you problem can be solved with some form of casting or another.
-
to be honest i get a lot of errors, ecspecially w/ the lines like
if ((inPTR) == '/' || (inPTR)== '*')
if(inPTR == ' " ')
the errors say something like error: no conversion from const* to int
-
Perhaps you mean to dereference the pointer first?
-
i tried that and still keep getting the same errors
-
inPTR is a const meaning you can't change it. You do that mean you add one to it to see the next char. Do this instead
Code:
char *inPTR=const_cast<char*>(in);
This takes away the const-ness of in so you can modify it. This really takes away the purpose of having in as const except to assure the user that it won't change.
Also, use for loops, and how about single quotes? You only handle double quotes....
-
HA HA i got it, i was dereferencing the wrong things,
thanks for the help guys
-
Quote:
Originally posted by Speedy5
inPTR is a const meaning you can't change it. You do that mean you add one to it to see the next char. Do this instead
Code:
char *inPTR=const_cast<char*>(in);
This takes away the const-ness of in so you can modify it. This really takes away the purpose of having in as const except to assure the user that it won't change.
Also, use for loops, and how about single quotes? You only handle double quotes....
No, you are incorrect on this.
const char* ptr;
doesn't say that the POINTER is constant, it just says that the data it POINTS to is constant. You can alter the value of a const char*, just not the value of what it points to.
In order to create a constant POINTER, you put the const after the * like:
char* const ptr; // constant pointer to a char
or
const char* const ptr; // constant pointer to a constant char
-
interesting....
this is why i love c++