Thread: strcat() added an unnecessary colon

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    49

    Question strcat() added an unnecessary colon

    Code:
    *txt_ptr++ = '\\';
    *txt_ptr++ = (char)strcat(txt_ptr, "\\927\\");
    I have these two lines of code within my program and the output I would hope to get would be: \927\

    However, the output I get after the second line is: \:927\

    Where did the colon come from and how do I prevent it from getting there? Thanks!

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Need more code. . . What are you trying to do?

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    49
    I just want my output to be \927\ rather than \:927\

    I'll try to get all the code that has to do with the output...unfortunately there's a lot of code that I have to sort through to figure out what is relevant...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well try removing the cast, then reading the manual page for what strcat actually does.

    Simply casting away all the warnings, then complaining about it not working is just dumb.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You're casting an address, which is a hex value, as a character and assigning it's value to memory. Do you know what happens when you cast a hex value as a character and assign it to memory? You get a character, and since you really don't know what that address you casted was, you're getting a random character, which in this case appears to be a colon.
    Code:
    Now when you have this:
    
            \\927\
    
    and you assign a colon 
           here
             v
            \\927\
    
    Guess what you get.
    Don't assign the result of strcat... and also, don't use strcat unless you're sure that your string is all NULLs, you could just being concatenating to the end of garbage.
    Sent from my iPadŽ

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > and the output I would hope to get would be: \927\
    Well the first line is useless, and the first half of the 2nd line is useless as well.

    All you need is strcat(txt_ptr, "\\927\\");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting question about strcat
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2009, 12:59 PM
  2. argv change
    By dracayr in forum C Programming
    Replies: 9
    Last Post: 04-10-2009, 02:49 AM
  3. strcat and a char
    By jjacobweston in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 04:10 PM
  4. strcat makes program crash
    By imoy in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 02:41 PM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-14-2001, 12:09 AM