Unsigned char* concatenation?

Programming, for all ages and all languages.
Post Reply
cg123

Unsigned char* concatenation?

Post by cg123 »

I've been having trouble concatenating unsigned char* values.. does anyone know of a way to do that?
xenos

Re:Unsigned char* concatenation?

Post by xenos »

What you need is a function like:

Code: Select all

char *strcat( char *strDestination, const char *strSource);
It normally searches the end of strDestination and copies the contents of strSource at that place. It should look similar to this one:

Code: Select all

char *strcat( char *strDestination, const char *strSource)
{
char* d = strDestination;
char* s = strSource;
while(*d) d++;
while(*s) *(d++) = *(s++);
*d = 0;
return(strDestination);
}
cg123

Re:Unsigned char* concatenation?

Post by cg123 »

Thanks, that works perfectly.. but now I'm having trouble clearing the value. Instead of being cleared before a new value is inserted, it just stacks up and eventually crashes the kernel.
Kemp

Re:Unsigned char* concatenation?

Post by Kemp »

If you mean that the characters being added on the end overwrite things then that's because the destination must have enough space allocated already to handle both strings. You could try adding them both to a third (larger) buffer if that's not possible to arrange.
cg123

Re:Unsigned char* concatenation?

Post by cg123 »

No, I mean I'm not able to clear out the char* 'command' with this:

Code: Select all

 command = "" 
It retains it's previous value.
Kemp

Re:Unsigned char* concatenation?

Post by Kemp »

You have to remember that a char* is not a string, the best case result for setting it equal to "" is that the first character will be made a null, thus pretending it is an empty string, the rest of it will still be there though.
proxy

Re:Unsigned char* concatenation?

Post by proxy »

cg123, no offence, but if you don't know how strings work in c (in that you are dealing with pointers to null terminated sequences of characters) and how to properly manipulate them, you have no business writing an OS.

This isn't meant to be hostile or insulting, just trying to suggest you do some more work with projects in user land until you REALLY know c/c++ well before diving into the kernel.

proxy
cg123

Re:Unsigned char* concatenation?

Post by cg123 »

The main reason I've been having trouble with variables is that I'm working on 3 different projects at once right now - one in PHP, one in Lua, and one in C++. I'm consistently getting the way variables work in them mixed up. :P I can see why you would say that, though.
Post Reply