String Concatenation Using Pointers No strcat()

This is a discussion on String Concatenation Using Pointers No strcat() within the C Programming forums, part of the General Programming Boards category; Hi there, I am having a bit of an issue when trying to concatenate strings using char * pointers. Here ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    1

    Thumbs down String Concatenation Using Pointers No strcat()

    Hi there,

    I am having a bit of an issue when trying to concatenate strings using char * pointers.

    Here is what I need:

    I need to add a list of ID's to a string that is a SQL statement so that I can end up with:
    SELECT DISTINCT Object_ID FROM Object_Interactions WHERE Object_ID IN ('UUID1', 'UUID2') ORDER BY Object_ID ASC;

    Here is what I am doing:

    Code:
    struct object_type
    {
        char	*id;		/* UUID = 32 chars length */
    };
    
    void strappnd(char *dest, char *src)
    {
    	/* appends one string to another */
    	while (*src != '\0')
    		*dest++ = *src++;
    
    	*dest = '\0';
    }
    
    char *buffer = NULL;
    char *sql = NULL;
    int j = 0;
    int buffer_size = 0;
    int object_count = 0;		
    
    object_count = 2;		/* this is actually a calculated value... */
    
    /* I have used malloc() and then assigned a value to object_list[j]->id and that was OK */
    
    buffer_size = 32 * object_count + strlen("''") * object_count + strlen(", ") * (object_count - 1) + 1;
    
    buffer = (char *)malloc(buffer_size * sizeof(char));	/* I know about pointer casting issue and malloc without stdlib.h but there is too much code to be changed... */
    strcpy(buffer, "");
    
    for (j=0; j<object_count; j++)
    {
    	printf("buffer=%s\n", buffer);
    	
    	strcat(buffer, "'");	
    	strappnd(buffer, object_list[j]->id);
    	
    	/* we must not end the sequence with a comma */
    	if (j < object_count - 1)		
    		strcat(buffer, "', '");
    	else
    		strcat(buffer, "'");
    }
    sql = (char *)malloc((100 + buffer_size + 1) * sizeof(char));
    sprintf(sql, "SELECT DISTINCT Object_ID FROM Object_Interactions WHERE Object_ID IN (%s) ORDER BY Object_ID ASC;", buffer);
    free(buffer);	
    
    printf("sql=%s\n", sql);
    Result:
    sql = SELECT DISTINCT Object_ID FROM Object_Interactions WHERE Object_ID IN (UUID1') ORDER BY Object_ID ASC;

    I have been trying and searching for a couple of days for all kinds of solutions but I can't seem to find anything and I can't make it work.
    Basically the question is how to transform a const char * into a char *. Does simple casting work? Is it safe? What else can I do because
    I keep hitting this wall, and I can't use the standard <string.h> functions because most of them expect a char const * as opposed to
    char * in one of the arguments.

    I have been looking on the Internet and forums, but for most cases strcat is being used and seems to be enough.
    I found interesting variations of strcat, but they don't seem to work and I don't have any more time for this. After finding libAPR I even
    thought of using it, but I only need one function from this library and plus the application must be portable on Linux, Mac, Windows...

    Any help on how to do string concatenation with char * or how to convert const char * to char * would be greatly appreciated.

    Other than that, C is just great.

    Thanks in advance,
    Ovidiu Anghelidi
    ovidiu@intelligencerealm.com

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It should be fine to pass ANY string INTO a const char * - it's only if you try to convert a const char * into a char * that you would be in trouble.

    E.g.
    Code:
    int strlen(const char *s)
    {
     ... 
    }
    
    int main() 
    {
       char s[10] = "A string";
    
       s[2] = ' ';
       s[3] = 't';
       s[4] = 'h';
    
       printf("%s is %d long\n", s, strlen(s));
       return 0;
    }
    This should compile and work fine.

    So I don't understand what the problem with const is in this case?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    >> Result:
    >> UUID1'
    Your code is showing the last "id" enumerated in the loop because "strappnd" is performing an "strcpy".

    Why not use strcat()? Is "id" not a null-terminated C-string?

    gg

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Codeplug View Post
    >> Result:
    >> UUID1'
    Your code is showing the last "id" enumerated in the loop because "strappnd" is performing an "strcpy".

    Why not use strcat()? Is "id" not a null-terminated C-string?

    gg
    If it wasn't, then it also wouldn't work for the current code, which detects end by comparing to zero.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 01:54 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

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