An interesting question about strcat

This is a discussion on An interesting question about strcat within the C++ Programming forums, part of the General Programming Boards category; Why the following code prints "Huh?" ? It seems that 0xFF is never appended to the list by strcat? Code: ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    An interesting question about strcat

    Why the following code prints "Huh?" ?
    It seems that 0xFF is never appended to the list by strcat?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    const char * const eof = "\0xFF";
    char list[2000] = {0};
    
    void append (const char * flower)
    {
        strcat (list, flower);
        strcat (list, eof);
    }
    int main()
    {
        const char *next;
        append("Calla Lily");
        append("Daisy");
        append("Tulip");
        next = strchr(list, 0xFF);
        if (!next) printf ("Huh?\n");
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Change this:
    Code:
    const char * const eof = "\0xFF";
    to:
    Code:
    const char * const eof = "\xFF";
    The former embeds a null character followed by "FF", thus nothing was concatenated.

    By the way, if this was supposed to be specifically C rather than the C part of C++, say the word and I shall move the thread. Otherwise, you should #include <cstdio> and <cstring> instead.
    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
    Apr 2007
    Posts
    284
    Thanks lot.
    1) Why strchr needs 0xFF with 0 at the beginning? Why not eof = "\0xFF"?
    2) Why strchr does not need to escape 0 ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Quote Originally Posted by meili100
    1) Why strchr needs 0xFF with 0 at the beginning? Why not eof = "\0xFF"?
    0xFF is an int. "\0xFF" is a string. If you want to use a character constant, then it should be '\xFF'.

    Quote Originally Posted by meili100
    2) Why strchr does not need to escape 0 ?
    It is not about strchr, but the fact that the literal is an int literal.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about an interesting Makefile
    By meili100 in forum Tech Board
    Replies: 2
    Last Post: 08-12-2008, 03:56 PM
  2. interesting question about new and nothrow new
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2008, 02:53 AM
  3. String Concatenation Using Pointers No strcat()
    By oviang in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 09:31 AM
  4. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM

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