#### Archived

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

# Need help with strings... Pleaze!!!

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

## Recommended Posts

Ok, it''s me again! I have some problems with processing strings. what I want to do is this: void CopyString(const char *String) { char *str; while (*String) { *str = *String; str++; String++; } } No matter what I do, at I get an access violation at the line *str = *String; Any ideas why this happens and what I coul do against it? Pleaze help! I''ve tried everything that came to my mind. I see no other solution than asking you guys.

##### Share on other sites
str is a pointer, hence it needs to point to some valid block of memory to hold the copy of the original data.

You could do this dynamically, eg:

str = new char[strlen(String)+1]; // allocate enough space for another copy

or you could assign it to a static buffer,

static char buf[100]; // some constant space large enough
str = buf; // and now point it to that

and then you can safely return it.

In the first case, you''ll want to delete[] the copy later. In the second, you don''t have to do that but each time you call this function the same buffer will be reused.

##### Share on other sites
I''m assuming you want to copy one string to another here.

If you want that function to work properlty, you''ll want to pass two parameters, not just one. In your code str is created as a local variable and won''t even see daylight, plus it never got allocated like AP said.

This is what you want:

  void CopyString(char *dest, const char *rrc){ char* Src=src; char* Dest=dest; while (*Src) { *Dest= *Src; Dest++; Src++; }}

I added in Src and Dest because I''m almost positive that the address is permanently changed after incimenting it like that. If I''m correct, your string would be "" after being passed through that function. Also, I don''t think you can incriment a const character can you?

##### Share on other sites
Naaahh,
actually, what I wanted is to process the string given by the argument.
It was meant to be an altered version of printf in fact, but I got around this using vsprintf in my function.
Don''t bother, I solved the problem.
thanx anyway.

• ### Forum Statistics

• Total Topics
628710
• Total Posts
2984319

• 23
• 11
• 9
• 13
• 14