Archived

This topic is now archived and is closed to further replies.

char* ???

This topic is 6063 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, I''m working on a very very simple routine to remove any spaces from an char* var and replace them with "_". I wrote this lil'' routine but I get a weird error(Using the free mingw compiler, cause I''m not allowed to install VC++ 6 so I downloaded Dev-C++ 4).
  har* ReplaceSpace(char* What) { int iTeller; for (iTeller = 0; iTeller < strlen(What); iTeller++) { if (What[iTeller] = " ") What[iTeller] = "_"; } return What; } 
The error I get is: 86 errorlog.cpp assignment to ''char'' from ''const char*'' lacks a cast Anyone??????? Sand Hawk Member of the Stupid Coders. http://www.stupidcoders.cjb.net -Earth is 98% full. Please delete anybody you can.

Share on other sites
first of all I see an error that you wrote, but that's not why you get that error

  if (What[iTeller] = " ") What[iTeller] = "_"; should be if (What[iTeller] == " ") What[iTeller] = "_";

second I don't think you need to return a value (in this case what)

and last I don't quite get your function name???

try this as a name

void FReplaceSpace(char* What)

Edited by - da_cobra on December 13, 2001 9:56:35 AM

Share on other sites
That''s a warning, not an error.

just cast it. return((const char*)What);

You also need to replace (What[iTeller] = " " with (What[iTeller] == " "

Share on other sites
if (What[iTeller] == '' '')
What[iTeller] = ''_'';

-------------------

dont return a value

the function will edit the character array you send it.

you might want to use for (iTeller = 0; What[iTeller] != 0; iTeller++)

for optimization.

Share on other sites
Also, I don''t believe Char * can be altered once assigned. You can re-assign it to a new string at will, but you can''t just alter it''s contents. It''s a pointer to a const string by definition.

Use a char array instead and return the pointer to that array. You should use the STL String class, they have functions for this already, you wouldn''t even need a function like this. If not, you''ll need the C string functions.

char * ReplaceSpace(char* What){

char temp[strlen(What)];
strncpy(temp,What,strlen(What)+1);
int iTeller;

for (iTeller = 0; iTeller < strlen(temp); iTeller++)
{
if (temp[iTeller] == '' '')
temp[iTeller] = ''_'';
}

return temp;

}

Of course, I just editted your code. Like some said before, you might just want to edit What and avoid a return if that''s ok with your main program. In that case, just need to say, "What = temp;" in your function and change the return type and return value of your function and you can just edit What on the fly in your program. But depends on what you want to do with What =)!

Also, I still say you look up STL String class. Safer, since CStrings are a major source of hacks and are prone to fence-post errors if not handled well. Plus, they got a built in replace functions to do exactly what you want.

G''luck.

R.

Share on other sites
Better yet, use std::string. Its much easier and you are less prone to errors.

ECKILLER

Share on other sites
"char *" does NOT imply "const". hence the need for the const qualifier.

const char *p; // means you cannot alter the contents of "p" (i.e. p[0] = ''a''.

char *p const; // means you cannot alter the value of "p" (i.e. p++.

just "char *" should be modifyable, unless Dev-C makes "char *" const by default.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share on other sites
"Also, I don''t believe Char * can be altered once assigned. You can re-assign it to a new string at will, but you can''t just alter it''s contents. It''s a pointer to a const string by definition."

Actually you can unless explicity said otherwise. its just like an int array you can arbitraly change the values in the array unless it was defined as const. before the function call if your value you pass in was declared as:
char * aString = "HI HOW ARE YOU" -->compiler make this const
then it wouldn''t work because it is constant chars, but if it was dynamically declared like:
char * aString = new char[15];
or
char aString[15];
then they wouldn''t be const chars and could be arbitraly changed. It all depends on how you wanted to use the method. if things passed in where defined like the first then there would have to be a copy created.

//ASSUMING THERE IS A \0 CHAR AT THE END
//FOR NONCONST CHAR *
void replaceCharWithChar(char & aString, char replace, char with)
{
for (char * charPtr = &aString; *charPtr != ''\0''; charPtr++)
{
if (*charPtr == replace)
{
*charPtr = with;
};
}
}

//FOR CONST CHAR *
char * replaceCharWithChar( char* oldString,
char replace ,
char with )
{
char * newString = new char[strlen(oldString) +1];
strcpy(newString, oldString);
for (char * charPtr = newString; *charPtr != ''\0''; charPtr++)
{
if (*charPtr == replace)
{
*charPtr = with;
};
}
return newString;
// REMEMBER TO DELETE THE STRING RETURNED WHEN FINISHED
}

I haven''t tested these so i''m not sure if there 100% functional

Share on other sites
discluding the return will make your code a little neater, so just cut out the return, like mentioned earlier. This should be the best version of this function:

void ReplaceSpace(char * String)
{
int iTeller;

for (iTeller = 0; iTeller < strlen(What); iTeller++)
{
if (What[iTeller] = " ")
What[iTeller] = "_";
}
//no return statement here!
}

You could also include a library such as . These libs are usually heavily optimized and can be quite helpful.

The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Share on other sites
  #include void ReplaceSpace(char *data){int i;for (i =0; data[i] != 0; i++){if (data[i] == ' ')data[i] = '_';}}int main(){char line[1000];sprintf(line,"Hello how are you.");ReplaceSpace(line);printf(line);return 0;}

Edited by - Mark_C on December 13, 2001 12:59:11 PM

1. 1
2. 2
Rutin
26
3. 3
4. 4
JoeJ
18
5. 5
gaxio
11

• 14
• 22
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631763
• Total Posts
3002197
×