473,387 Members | 1,812 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,387 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. };
  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 4409
weaknessforcats
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

37
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...
22
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...
6
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...
23
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;
35
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...
5
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
87
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...
34
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. ...
8
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) {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.