Copying part of a string in C

Posted by wolfPack88 on Stack Overflow See other posts from Stack Overflow or by wolfPack88
Published on 2010-06-08T21:48:58Z Indexed on 2010/06/08 21:52 UTC
Read the original article Hit count: 336

Filed under:
|

This seems like it should be really simple, but for some reason, I'm not getting it to work. I have a string called seq, which looks like this:

ala ile val

I want to take the first 3 characters and copy them into a different string. I use the command:

memcpy(fileName, seq, 3 * sizeof(char));

That should make fileName = "ala", right? But for some reason, I get fileName = "ala9". I'm currently working around it by just saying fileName[4] = '\0', but was wondering why I'm getting that 9.

Note: After changing seq to

ala ile val ser

and rerunning the same code, fileName becomes "alaK". Not 9 anymore, but still an erroneous character.

© Stack Overflow or respective owner

Related posts about c

    Related posts about string-manipulation