strcat() and null chars question

This is a discussion on strcat() and null chars question within the C Programming forums, part of the General Programming Boards category; Working through Teach Yourself C in my spare time and am now in chapter 5 which deals with arrays and ...

  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
    21,462
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    50
    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
    333
    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.
    http://www.malcolmmclean.site11.com/www

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,211
    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%.

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, 05: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21