Thread: concatenating single chars to multi char arrays

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    15

    concatenating single chars to multi char arrays

    Hi

    I have a problem assigning a single character into a pointer and an array larger than 1 - I think I know why this is but I am unsure about how to overcome it...

    Code:
     char TempChar[3]
     char SinChar[1];
    
    // At any time StoreChar could contain 1 to 3 characters 
    // - I want whatever characters are stored in it to be assigned 
    //to *data - I think the interger cast causes problems but it is 
    //necessary - how can I convert TempChar to int without messing 
    //up the data?
    
    *data= int(TempChar);
    
    //Later on I wish to concatenate SinChar (a single char 
    // array) to TempChar - this seems to work but always has 
    // nonsense characters as well - I think this is due to a single 
    //character being concatenated to a array which could contain 
    //either 1 or 2 characters - how can I do this without the 
    //nonsense characters?
    
    strcat(TempChar, SinCheck);
    Any help much appreciated!

    Jimbo

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char SinChar[1];
    It needs to be
    char SinChar[2]; // a single char as a string (don't forget the \0)

    SinChar[0] = theChar;
    SinChar[1] = '\0';

    Then you can strcat() it to whatever you want, providing there is room to append a char.
    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.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Adding to Salem's description, strcat() operates on strings, not single characters. And by definition a string must have a null or 0 byte at the end of it. Therefore, your 'single character' must be two characters, 2nd char is 0.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    7

    Somewhat faster and less artificial solution.

    Code:
    #define strcatchar(__str,__cc) \
    { \
        int  __slen; \
        __str[__slen = strlen(__str)] = __cc; \
        __str[__slen+1] = '\0'; \
    }

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Somewhat faster and less artificial solution.

    Originally posted by qed
    Code:
    #define strcatchar(__str,__cc) \
    { \
        int  __slen; \
        __str[__slen = strlen(__str)] = __cc; \
        __str[__slen+1] = '\0'; \
    }
    Loose the __'s. They make the code almost unreadable.
    Code:
    #define strcatchar(str,cc) \
    { \
        int  slen; \
        str[slen = strlen(str)] = cc; \
        str[slen+1] = '\0'; \
    }
    will suffice.

    and the line str[slen = strlen(str)] = cc; is a nice trick but obfuscates the routine. a simple
    slen = strlen(str);
    str[slen] = cc;

    does the job just as well without the confusion.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    7
    The __'s are used to try to keep the variable names out of regular the program usable namespace. For example, try the following with your macro:
    Code:
    char * slen = malloc (16);
    *slen = '\0';
    strcatchar(slen, 'x');
    The two equals on a line may be a little unusual, but there is nothing actually obfuscating about it. The order of operations is strongly implied, and the natural expectation is the correct one.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > The __'s are used to try to keep the variable names out of regular the program usable namespace
    Except now you've trampled over the system namespace. All identifiers with a leading _ are reserved.

    Since you introduce a new scope with the braces, there is no problem with using slen
    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.

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    7
    Except now you've trampled over the system namespace. All identifiers with a leading _ are reserved.
    Yes, I realize this. But there is no way to make an inaccessible dynamic name space inside a C macro, so I just chose the one that the non-system programmer will avoid. Making it work with any particular compiler is just a matter of porting it.

    Since you introduce a new scope with the braces, there is no problem with using slen
    Yeah, but look at my example. The results will not be the code that was intended.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM