This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

"problems" with strcat

Hello Everybody
I want to attach one String to another.
The String that I want to attach is for example: {0x01,0x02,0x03,0x00,0x05}

the strcat doesn't copy the bytes after 0x03 because it detects 0x00 as the NUL-Terminator I guess.

its the same when I use strncat with the right length parameter.

...but I really need to handle strings like that!

Who can help?

Thanx a lot

Parents Reply Children
  • I know the "mem**.." command would be the right one for byte-arrays.

    The Problem is that they overwrite the 1st array and are not attached.
    I have 2 arrays (both with a variable and unpredictable length)
    I wanted to use it for framing where I can put /Header/OptionA[20]/OptionB[5]/OptionC[10]/

    but also /Header/OptionC/OptionA/

    I think I'll have to do it with counters and construct every concatenation by myself.


  • If the data in your arrays has no predictable terminator value (like the null character in C strings), then you'll have to remember the length of the data.

    To concatenate, all you have to do is a bit of pointer arithmetic on your destination.

    int memcat (U8* dst, int dstLen,
                U8* src, int srcLen)
        {
        return memcpy (dst + dstlen, src, srcLen);
        }
    

    Note that you'd better be sure that dst points to a buffer big enough to hold all this data.

    Also remember that adding '1' to a pointer in C adds not one byte to the address, but sizeof(TypePointedTo). So if you have

    typedef struct
       {
       ... // various fields here
       } Header;
    
    Header* hdr;
    

    then to point just past the header, you'd say "hdr + 1", not "hdr + sizeof(Header)". Of course, if your TypePointedTo happens to be a byte, then "+ 1" does what you might have expected.

    If you like, you can gussy up the basic concept by putting the variables together in a structure.

    typedef struct
       {
       int maxLen;
       int curLen;
       U8* data;
       } BoundedArray;
    

    and then write your routines to pass those structures around to appropriate functions built on top of memcpy().

  • "The Problem is that they overwrite the 1st array and are not attached."

    Again, the problem lies in your code - not the library functions!

    memcpy copies the data you specify to the destination which you specify - so if you specify a destination that will overwrite your 1st array, that's what'll happen!
    If you don't want it to overwrite your 1st array, then specify the destination address accordingly!

    This is the You Asked For It, You Got It! (YAFIYGI) programming paradigm! ;-)

  • Thanx for all that help! The code now does what I want.
    Have a nice week