473,246 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

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. };
  6. void strappnd(char *dest, char *src)
  7. {
  8.     /* appends one string to another */
  9.     while (*src != '\0')
  10.         *dest++ = *src++;
  12.     *dest = '\0';
  13. }
  15. char *buffer = NULL;
  16. char *sql = NULL;
  17. int j = 0;
  18. int buffer_size = 0;
  19. int object_count = 0;        
  21. object_count = 2;        /* this is actually a calculated value... */
  23. /* I have used malloc() and then assigned a value to object_list[j]->id and that was OK */
  25. buffer_size = 32 * object_count + strlen("''") * object_count + strlen(", ") * (object_count - 1) + 1;
  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, "");
  30. for (j=0; j<object_count; j++)
  31. {
  32.     printf("buffer=%s\n", buffer);
  34.     strcat(buffer, "'");    
  35.     strappnd(buffer, object_list[j]->id);
  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);    
  47. printf("sql=%s\n", sql);
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 4401
9,208 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

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

Similar topics

by: Shri | last post by:
hi all, i am writing a code in which i have a char buffer "cwdir" which hold the current working directory by calling the function getcwd(). later i change the directory to "/" as i have to make...
by: jacob navia | last post by:
A function like strcpy takes now, two unbounded pointers. Unbounded pointers, i.e. pointers where there is no range information, have catastrophic failure modes specially when *writing* to main...
by: Gary Morris | last post by:
Hi all, I tried posting this through a free news server, but it still has not appeared in Google, so if it turns up again I apologize. I hope someone can help me with this, or at least help...
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your...
by: kaming | last post by:
Dear all, I would like to ask is these any DB2 function that can concatenation strings and have grouping capability?? for example, Tables:T F1 F2
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
by: Sivakumar Kotamraju | last post by:
Hi, this is the code to print s. main() { sample s1(10,20,30); char *s; s="Hello"+s1; } friend char * operator +(char *s,sample &b) {
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.