468,512 Members | 1,288 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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:
Expand|Select|Wrap|Line Numbers
  1. struct object_type
  2. {
  3.     char    *id;        /* UUID = 32 chars length */
  4. };
  5.  
  6. void strappnd(char *dest, char *src)
  7. {
  8.     /* appends one string to another */
  9.     while (*src != '\0')
  10.         *dest++ = *src++;
  11.  
  12.     *dest = '\0';
  13. }
  14.  
  15. char *buffer = NULL;
  16. char *sql = NULL;
  17. int j = 0;
  18. int buffer_size = 0;
  19. int object_count = 0;        
  20.  
  21. object_count = 2;        /* this is actually a calculated value... */
  22.  
  23. /* I have used malloc() and then assigned a value to object_list[j]->id and that was OK */
  24.  
  25. buffer_size = 32 * object_count + strlen("''") * object_count + strlen(", ") * (object_count - 1) + 1;
  26.  
  27. 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... */
  28. strcpy(buffer, "");
  29.  
  30. for (j=0; j<object_count; j++)
  31. {
  32.     printf("buffer=%s\n", buffer);
  33.  
  34.     strcat(buffer, "'");    
  35.     strappnd(buffer, object_list[j]->id);
  36.  
  37.     /* we must not end the sequence with a comma */
  38.     if (j < object_count - 1)        
  39.         strcat(buffer, "', '");
  40.     else
  41.         strcat(buffer, "'");
  42. }
  43. sql = (char *)malloc((100 + buffer_size + 1) * sizeof(char));
  44. sprintf(sql, "SELECT DISTINCT Object_ID FROM Object_Interactions WHERE Object_ID IN (%s) ORDER BY Object_ID ASC;", buffer);
  45. free(buffer);    
  46.  
  47. printf("sql=%s\n", sql);
  48.  
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
<e-mail address removed by weaknessforcats>
Dec 7 '07 #1
1 4123
weaknessforcats
9,207 Expert Mod 8TB
strappnd(buffer, object_list[j]->id);
My guess is the bug is here. Your strappnd() function does not append to the end of the string. It appends starting at the pointer address you give it, buffer in this case instead of the end fo the string in buffer.

Remove your strappnd() and replace it with strcat().

BTW: All of these C string functions in string.h have been deprecated by Microsoft and are not to be used in Windows code. There are safe versions of these but that will make the Windows version of your code not portable to Linux/Unix.
Dec 7 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

37 posts views Thread by Shri | last post: by
22 posts views Thread by jacob navia | last post: by
6 posts views Thread by Gary Morris | last post: by
23 posts views Thread by Nascimento | last post: by
35 posts views Thread by michael.casey | last post: by
5 posts views Thread by kaming | last post: by
87 posts views Thread by Robert Seacord | last post: by
34 posts views Thread by Larry Hastings | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.