Thread: strcat() and null chars question

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    strcat() and null chars question

    Working through Teach Yourself C in my spare time and am now in chapter 5 which deals with arrays and strings. My question is fairly obvious but the author does not seem to address it in his treatment of strcat(). What I want to know is this: all strings contain a null character at the end, which strlen() does not return. When using strcat() to combine two strings, are both null characters preserved? Or is one of them lost, so that only one null character exists at the end of the new, larger string? Thanks in advance for your time and patience in answering this novice's question.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by David Reghay
    When using strcat() to combine two strings, are both null characters preserved? Or is one of them lost, so that only one null character exists at the end of the new, larger string?
    One of them is "lost". After all, the second one wouldn't matter anyway since the string length, as you noted, is only until (and excluding) the first null character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    The man page for strcat(3) describes it well:

    char *strcat(char *dest, const char *src);
    ...
    The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte.
    The man pages are great for reference. In Linux, just type "man <function>" in term, otherwise do an online search.

    If you want to see how the strcat() function is coded, you can always download the source code and check it out. For glibc, it's located in "string/strcat.c".

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by fnprintf View Post
    The man page for strcat(3) describes it well:


    The man pages are great for reference. In Linux, just type "man <function>" in term, otherwise do an online search.

    If you want to see how the strcat() function is coded, you can always download the source code and check it out. For glibc, it's located in "string/strcat.c".
    It's probably a better exercise to write it yourself. Call it mystrcat(), and make sure it always does exactly the same a strcat, even when passed an empty string or the null pointer. (You can't reliably replicate behaviour when passed a wild pointer or an unterminated string as that's undefined).
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Malcolm McLean View Post
    It's probably a better exercise to write it yourself. Call it mystrcat(), and make sure it always does exactly the same a strcat, even when passed an empty string or the null pointer.
    strcat() has undefined behaviour if passed a null pointer. So, if the only observable difference between mystrcat() and strcat() is in what happens when they are passed a null pointer, they are equivalent - at least, according to the standard.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers of chars and arrays of chars Question
    By shiroaisu in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2010, 10:42 AM
  2. question about strcat() in C
    By thungmail in forum C Programming
    Replies: 2
    Last Post: 11-07-2009, 06:57 AM
  3. Replies: 2
    Last Post: 10-30-2009, 08:55 AM
  4. Question about strcat
    By Countfog in forum C Programming
    Replies: 5
    Last Post: 05-04-2008, 03:16 PM
  5. Strcat question
    By Berix in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2005, 01:00 PM

Tags for this Thread